RegExp Serverside code not working like we expect and works in other areas such as browsers <script>

saltemus
Kilo Contributor

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

 

 

1 ACCEPTED SOLUTION

Sri Harsha3
Tera Expert

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); 

View solution in original post

1 REPLY 1

Sri Harsha3
Tera Expert

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);