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

Hi David,

regular expression should work fine when you know the pattern would match everytime

I would recommend string manipulation for simpler cases

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

I would say both works without any specific Pros/Cons. Just a matter of personal preference, I think. So use what you feel most comfortable using. I would recommend learning RegEx for future cases since it can be powerful, and you will probably have a requirement in the future that RegEx is the solution for the requirement.

I'm no expert in RegEx, but when I started, I began with Chucks, both videos about it. I can't find them right now, but I bet @Chuck Tomasi have the links somewhere 😃 

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

TechNow Episodes 31 and 32 on Regular Expressions can be found on the master list.

Thanks @Chuck Tomasi and @Göran Lundqvist  !!!

Can I modify this one to have a set end string?
Values in between will change.
?version and api will be the constant
original - ?version=1&modificationDate=1412296476867&api

Desired -  ?api