Regular Expression with j option

lonesoac01
Giga Guru

Hello all,

 

I am attempting to use Regular Expressions.  I am reading the documentation of: https://docs.servicenow.com/bundle/sandiego-application-development/page/script/general-scripting/co... and it says that I can use the j option to use for JavaScript based Regular Expressions.  I am under the impression that I can use the g and j options with regular expressions.  I have attached two screenshots.  One screenshot just has the g option and SN does not complain.  I then try to add the j option, and SN complains.  What am I missing here?

1 ACCEPTED SOLUTION

When posting the code just be sure to format it by clicking the three dots icon, then the </> icon and select JavaScript.  Should post without issue.

 

I just ran a test script against all 4 and it works using the j.  I do get the orange ! in the editor, but it still works.

var re = new RegExp("(?<!HSF)[0-9]{3}( |-|)[0-9]{2}( |-|)[0-9]{4}(?![0-9])", "jg");
var testCases = ['123121234','lksjfd 123121234 lksjdf','hello world 123121234 bleh bleh bleh','you are a ding dong. HSF123121234'];
for (var i in testCases) {
	gs.info('{0} = {1}', testCases[i], testCases[i].match(re).join(','));
}

 

Aoife

View solution in original post

3 REPLIES 3

Aoife
Tera Guru

Can you share the code you are using?  I'll see if I can figure it out for you.  

Aoife

I have attached the regular expression in the attachment in this reply.  I am terrified to post the actual code in the body since another post of mine was flagged as Spam because of my regular expression.  What I am trying to accomplish with this regex is a Negative Lookbehind.  I am attempting to ignore the strings that start with HSF.

 

Here is a simple data set:

123121234

lksjfd 123121234 lksjdf

hello world 123121234 bleh bleh bleh

you are a ding dong. HSF123121234

 

The only example that should be ignored is the last line.

When posting the code just be sure to format it by clicking the three dots icon, then the </> icon and select JavaScript.  Should post without issue.

 

I just ran a test script against all 4 and it works using the j.  I do get the orange ! in the editor, but it still works.

var re = new RegExp("(?<!HSF)[0-9]{3}( |-|)[0-9]{2}( |-|)[0-9]{4}(?![0-9])", "jg");
var testCases = ['123121234','lksjfd 123121234 lksjdf','hello world 123121234 bleh bleh bleh','you are a ding dong. HSF123121234'];
for (var i in testCases) {
	gs.info('{0} = {1}', testCases[i], testCases[i].match(re).join(','));
}

 

Aoife