- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2022 08:02 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2022 11:39 PM
Hi
sorry my bad. Please use the following modified expression
/Address:([\S\s]+?)Phone Number/g
Kind regards
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2022 08:38 PM
Hi
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2022 11:28 PM
Hi
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2022 11:39 PM
Hi
sorry my bad. Please use the following modified expression
/Address:([\S\s]+?)Phone Number/g
Kind regards
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2022 04:01 AM
Hi
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