Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Utilize RegExp in Inbound email action

Afsar2
Tera Contributor

 

Hi guys,

I am trying to utilize regular expression in an Inbound Email Action script.

I am trying to parse an email body (email.body_text) and extract few line between 2 keywords.

Regular Expression Pattern: '/Address:(.*?)Phone Number/s'

The above pattern is valid verified at https://regex101.com/

Syntax in the script is as follows.

var regExp = new RegExp('/Address:(.*?)Phone Number/s');
var body_text = email.body_text.toString();
var extract_address = body_text.match(regExp);

 

in the log statement "extract_address" is null.

I am not sure why it is happening so.

Any suggestions will be of great help.

Thank you

 

 

 

1 ACCEPTED SOLUTION

Hi @Afsar 

sorry my bad. Please use the following modified expression

/Address:([\S\s]+?)Phone Number/g

Kind regards
Maik

View solution in original post

6 REPLIES 6

Maik Skoddow
Tera Patron
Tera Patron

Hi @Afsar 

please use the following approach:

 

var regExp = /Address:(.*?)Phone Number/g;
var body_text = 'My Address: In the Sky 10, Heaven, Phone Number: 777';
var arrMatches = regExp.exec(body_text);

if (Array.isArray(arrMatches)) {
  gs.info(arrMatches[1]);
}
else {
  gs.warn('Nothing found');
}

 

Kind regards
Maik

Hi @Maik Skoddow ,

Thanks for your suggestion.

Your solution works perfectly fine when string is in single line.

But my string has multiple newline characters.

I need a regular expression which can fetch the text between 2 keywords over multiple lines.

 

When I use this '/Address:(.*?)Phone Number/s'. It actually throwing an error in SNOW for s flag as invalid.

Thank you.

Hi @Afsar 

sorry my bad. Please use the following modified expression

/Address:([\S\s]+?)Phone Number/g

Kind regards
Maik

Hi @Maik Skoddow ,

 

This is correct answer. 

The flag '\s' is introduce with ES2018 and servicenow suports ES2009 (ES5).

So I tried converting ES5+ code to ES5 here https://mothereff.in/regexpu.

I got the regex pattern as you suggested.

 

Great Thanks! 

 I appreciate your help.

 

Regards,
Afsar Sheikh