Using SNC Regex API to enhance performance

georgechen
Kilo Guru

Hi folks,

Anyone can assist me with a SNC Regex expression enhancement, currently I have the regular Java Script email regular checking which has overwhelmed our Non-Production instances, I have been advised to use SNC Regex API to reduce the performance impact according to SNC Regex API http://wiki.servicenow.com/index.php?title=SNC_Regex_API#gsc.tab=0

The code I am using is

var emailAddresses = email.body_html.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);

and I am trying to rewrite this in SNC format, so here is my attempt

var rgx = new SNC.Regex('[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}');

var result = 'george.chen@abcdefg.com';

gs.print(result);

gs.print(rgx.match(result));

However, the above code has thrown an error in ServiceNow background script.

Any help is appreciated

Kind regards,

3 REPLIES 3

Robert Beeman
Kilo Sage

Regex needs to start and close with a forward slash (like you have in your original regex). Also, you need an "i" modifier to make it case in-sensitive sinse your new regex is only matching on capital letters.



Replace your first line with this and it should work:


var rgx = new SNC.Regex('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i');


Your original regex has a "g" modifier as well so that it is global and will not stop on first match so you might want to add that back in as well.



But, as I mentioned in the other thread, SNC Regex API may not be recommended if you are on a newer version (Eureka Patch 5 or later) of SN.


James_Neale
Mega Guru

Hey George,



Agree with Robert on the SNC Regex API not being recommended - I've never even found it necessary. Newer ServiceNow versions automagically intercept calls to new RegExp('abc') and /abc/ and returns them as SNRegExp objects.



You would need to add the global modified (g) in as well as you want to match all email addresses from the email and not just the first one found.



There is virtually no difference between your new one and the old one. The old one just instantiates a new expression inline, caters for upper/lower case using a-zA-Z (as well as the case-insensitive 'i' flag - unnecessary?) and matches globally (g modifier), which should be absolutely fine. I can see this being an issue if you are running it on a very large number of very large emails as the 'g' modifier means it won't quit searching until the end of the string.



The only other thing I'd suggest is considering where you are running the code and the string you are running it on. If you only care about the body that's just come in for example, you may want to split off the reply email and just run the expression on the most recent email body. (See system property glide.pop3.reply_separators)



You can also test your scripts and regular expressions more easily using Xplore - download for free on Share.



Cheers
James



P.S. If you want super duper email regex matching, then check out the JavaScript matcher here: http://emailregex.com/


Thanks James, I see using the SNC Regex API is obsolete.   The code I am running is in an inbound action script with the highest priority which comes to play at first place against every single email.     The regular JavaScript Regex I use is causing a performance impact that has overloaded the email processing node.   We had to raise an ServiceNow HI call to restart it, and they also advised to deactivate the Inbound Scripts.   We endued up with the idea of optimising the regex or other ServiceNOW OOTB APIs to hopefully address this.



Before using Regex, I was writing simple loop code to extract email addresses in an email, would this make the process more efficient instead of the Regex?


e.g



var emails = "abc@abc.com; george.chen@tester.com"; something like



while (emails.indexOf('@') >0) {



    var email = email.substring(emails.indexOf('@'), emails.indexOf(';')-1);


    // processing user by email address


      var user = new QSS_GetUserByEmail().getUserByEmail(email);


    // processing user......



    emails = email.substring(emails.indexOf('@'));


}