Event Rule Regex Parsing
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 10:19 AM
I`m trying to create a regex to extract a server tag from a tags list. The tags field is similar to the string below?
[[CONTEXT]OU:IT, [CONTEXT]NAME:xyz.internal, [CONTEXT]KIND:kind1]
or
[[CONTEXT]OU:SALES, [CONTEXT]NAME:xyz.internal]
I`m using the regex below:
.*\[CONTEXTLESS\]CI:(.*?)[,\]]
The problem I'm facing is I expect a result like "xyz.internal" but I'm getting the result "xyz.internal, [CONTEXT]KIND:kind1"
I have tested it on regex101.com and it works as expected there, but not on ServiceNow Event Rule.
Any help would be appreciated
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2023 11:21 PM
Did you find any solution for this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2023 11:55 PM
Hi @Ronan Lucio Per ,
I trust you are doing great.
Here's an updated regex pattern that should give you the desired result:
\[CONTEXT\]NAME:(.*?)(?:,|\])
This pattern will match the string "[CONTEXT]NAME:" followed by any characters (non-greedy) until it encounters either a comma or a closing square bracket.
Here's an example of how you can use this regex pattern in ServiceNow:
// Assuming the tags list is stored in a variable called 'tags'
var tags = '[[CONTEXT]OU:IT, [CONTEXT]NAME:xyz.internal, [CONTEXT]KIND:kind1]';
var regexPattern = /\[CONTEXT\]NAME:(.*?)(?:,|\])/;
var matches = regexPattern.exec(tags);
// 'matches' will contain an array where the first element is the complete match,
// and the second element is the extracted server tag
if (matches && matches.length >= 2) {
var serverTag = matches[1];
gs.info('Extracted server tag: ' + serverTag);
} else {
gs.info('No server tag found in the tags list.');
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi