The CreatorCon Call for Content is officially open! Get started here.

Populating a String field with another string field but with some restriction

Lavanya Nagendr
Giga Guru

Hi All,

 

I have a requirement where i need to get the "Descripion" field and auto populate another field lets say."Description 1" but without any numbers.

 

For example:

 

if Description ----is--> General Requirement 1.1

then Description 1 ---should be---> General Requirement

 

Note: The content on the Description may vary and the numbers like(1.1 or 1.04) will always be at the end.

1 ACCEPTED SOLUTION

Hi @Lavanya Nagendr 

 

You can put like below

 

var description = "General Requirement 1.1  @%";

description = description.replace(/[0-9.]/g, '');

var description_1= description;
Regex explaination :
0-9 - check numbers
.      - check dot(.)
/g  - search entire string
 
You can use /[0-9.@#$%^]/g if you have special characters as well
 
Output : 
 
VishalBirajdar7_0-1694777022537.png

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

6 REPLIES 6

Daniel Borkowi1
Mega Sage

Hi @Lavanya Nagendr , use a RegEx and replace function for that.

 

var str = g_form.getValue('description');
var result = str.replace(/([^a-z|^A-Z|^\s]*)/gm,"").trim();	
g_form.setValue('u_description_1', result);

 

Greets
Daniel

Please mark reply as Helpful/Correct, if applicable. Thanks!

 

Harish Bainsla
Kilo Patron
Kilo Patron

var str = g_form.getValue('description');
var result = str.replace(/([^a-zA-Z\s]*)/gm,"").trim();
g_form.setValue('u_description_1', result); 

https://docs.servicenow.com/en-US/bundle/rome-application-development/page/script/general-scripting/...

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB1393773