- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-03-2019 04:43 PM
We are attempting to use the global identifier flag in ServiceNow to return multiple matches in a string (not just the first) while executing regular expressions. We can't seem to get /g working in the expressions.
It seems as if ServiceNow Server Side Scripting does not support the different methods we have tried (Shown below).
Using Jakarta (yeah we are upgrading to london real soon)
Background Scripting used to test. These methods ran in the server side.
Example:
Method 1 (does work but replaces):
var string = ‘test test’;
var regEx = new RegExp(/test/g);
var newString = string.replace(regEx, ‘cookie’);
gs.log(newString); // *** Script: cookie cookie
Method 2 (does not work😞
var string = ‘test test’;
var regEx = new RegExp(/test/g);
var regExArr = regEx.exec(string);
gs.log(regExArr.length); // *** Script: 1
Method 3 (does not work😞
var string = ‘test test’;
var regEx = new RegExp(‘test’,’g’);
var regExArr = regEx.exec(string);
gs.log(regExArr.length); // *** Script: 1
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2019 02:56 AM
I have done like this in the console it is working
var string = 'test test';
var regEx = /test/g;
var regExArr = string.match(regEx);
console.log(regExArr);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2019 02:56 AM
I have done like this in the console it is working
var string = 'test test';
var regEx = /test/g;
var regExArr = string.match(regEx);
console.log(regExArr);