Remove characters in a string AFTER specific characters

David Casper
Tera Guru

I'm needing to update the names of some assets and CIs by removing some ending characters. I need to look for ' - ' (that's space - space) and then remove the ' - ' plus the characters after that. 

Have not had to do this before, still looking into it. Thanks in advance for the assistance!

1 ACCEPTED SOLUTION

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Hi David,

You can do this by regEx and replace. Here is an example you can test and reuse pretty much:

var test = 'goran - remove this';

gs.debug(test.replace(/\s[-]\s.*/, ''));

//Göran
Feel free to connect:
LinkedIn
Subscribe to my YouTube Channel
or look at my Book: The Witch Doctor's Guide To ServiceNow

View solution in original post

9 REPLIES 9

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Hi David,

You can do this by regEx and replace. Here is an example you can test and reuse pretty much:

var test = 'goran - remove this';

gs.debug(test.replace(/\s[-]\s.*/, ''));

//Göran
Feel free to connect:
LinkedIn
Subscribe to my YouTube Channel
or look at my Book: The Witch Doctor's Guide To ServiceNow

I was going down the same path. Really appreciate the quick reply. 

This is what I was testing successfully so far. 

var text = 'test - remove';
gs.print(text.split(' - ')[0]);

What are your thoughts between the two?

Hi David,

the script looks fine

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

What are your thoughts between the two scripts listed? Pros/Cons? Just wanting to better understand.