Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to write RegExp "or" statement?

Edwin Fuller
Tera Guru

I currently have the below business rule that searches the description field to find "Password: xxxxxxxxx" and removes the characters that follow the colon and replaces it with "Password Removed" So If I was to type "Password: TestPW!" the business rule below would replace it with "Password: Password Removed". I need to write an or statement to check for "Password:xxxxxxxxx" and perform the same replace action.

Currently searching for "Password: 123456789" with space after the colon

Need to add an or statement to search for "Password:123456789" with no space after the colon

Current Business Rule

(function executeRule(current, previous /*null when async*/) {    

  var rgx = new SNC.Regex('/Password: [0-9]*/');  

  var result = current.description;  

  current.description = rgx.replaceAll(result,"Password: Password Removed");

})(current, previous);

1 ACCEPTED SOLUTION

I see. Well, there are a couple things.



1. The 'SNC Regex' method of doing this seems to not be the recommended way to proceed (see: Convert SNC Regex expressions to enhanced regex expressions).



2. I rewrote the script using the above suggestions, but encountered the same issue. I don't usually use SNC.Regex api (or the plain javascript regExp constructor); I do it inline, so to speak.



In our instance, replacements are made in the following method. I haven't found any issues doing it this way, but someone else might be able to chip in on the best practices. In your case it is working as expected.



var result = current.description;


current.description = result.replace(/Password:[\s]?[\w!]*/g, "Password: Password Removed");


View solution in original post

7 REPLIES 7

I see. Well, there are a couple things.



1. The 'SNC Regex' method of doing this seems to not be the recommended way to proceed (see: Convert SNC Regex expressions to enhanced regex expressions).



2. I rewrote the script using the above suggestions, but encountered the same issue. I don't usually use SNC.Regex api (or the plain javascript regExp constructor); I do it inline, so to speak.



In our instance, replacements are made in the following method. I haven't found any issues doing it this way, but someone else might be able to chip in on the best practices. In your case it is working as expected.



var result = current.description;


current.description = result.replace(/Password:[\s]?[\w!]*/g, "Password: Password Removed");


Thanks a lot for this information Dylan. I've tested your solution and it is working beautifully, exactly what I need it to do. So much simpler too


Edwin,



Great, glad to help!