Find a value between two strings using regex

Aidan
Tera Guru

(originally posted under Now Platform)

I'm looking to parse a script field to find any messages that have been referenced in gs.getMessage() calls.  The following regex works fine in any online test website but not in ServiceNow;

(?<=getMessage\(\").*?(?=\")

Here's the code I'm trying to use;

var regex = new RegExp(/(?<=getMessage\(\").*?(?=\")/g);

which returns the following error when tested/exec'd

Evaluator: com.glide.script.RhinoEcmaError: Invalid quantifier ?
   Line(0) column(0)

Looks to me like the Positive Lookbehind is failing, but not sure of an alternative.

 

5 REPLIES 5

Brian Lancaster
Tera Sage

I check your reg expression at https://www.regextester.com/ and I get a message there the lookbehind is not supported in Javascript.

Aidan
Tera Guru

The message I was getting was that it wasn't supported in all browsers.  Are there any alternatives then?

I'm not sure I follow you requirements.  Can you give more details?

Sure, essentially I'm looking to scan through the script field for various records to see what custom messages  are being referenced.  So if there is a script such as the following;

/* display message */
alert(getMessage("Custom text message"));

/* get message and store */
var msg = getMessage("Store custom message");

I want to have the following returned;

Custom text message
Store custom message