- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2022 11:24 PM
Hello,
I want to use regex to match pattern on my BR.
I defined a system property in sys_properties
name : match.pattern.1
value : /^([A-Z]{4})+([-]{1})+([0-9]{4})+([-]{1})+([0-9]{7})$/
this is the code
var toMatch = "WWW-0000-0000000";
var pattern = gs.getProperty(match.pattern.1);
if(pattern.test(toMatch)){
print true;
}else{
print false;
}
I'm getting false
But when I define pattern directly in code I get true.
var toMatch = "WWW-0000-0000000";
var pattern =/^([A-Z]{4})+([-]{1})+([0-9]{4})+([-]{1})+([0-9]{7})$/;
if(pattern.test(toMatch)){
print true;
}else{
print false;
}
I get true
Why regex is not working when pattern is from properties?
Thank you
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2022 11:59 PM
In addition to AnubhavRitolia's comment regarding the way your are referencing the sys_property.
You cannot utilize the sys_property value/string in exactly same method as you would a regex, because it is a plain old string not. Also your regex is incorrect and doesn't validate when tested externally
- I used https://regex101.com/ to validate it
Maybe try a sys_property value of
'([A-Z]{3})+([-]{1})+([0-9]{4})+([-]{1})+([0-9]{7})'
and consume it like
var toMatch = "WWW-0000-0000000";
var myAnswer = false;
var pattern = new RegExp(gs.getProperty('match.pattern.1'));
if(pattern.test(toMatch)){
myAnswer = true;
}
gs.info('myAnswer ' + myAnswer);
This works for me in a PDI
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2022 11:35 PM
Hi @naxis alx
As you have stored your Regular Expression in Property as String value, when you can it using gs.getProperty() it return in the form of String in backend like var pattern ="/^([A-Z]{4})+([-]{1})+([0-9]{4})+([-]{1})+([0-9]{7})$/";
But if you see it should be without Quotes (""). Due to this it won't work using Properties. You need to use it directly in your script as below:
var pattern =/^([A-Z]{4})+([-]{1})+([0-9]{4})+([-]{1})+([0-9]{7})$/;
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2022 11:59 PM
In addition to AnubhavRitolia's comment regarding the way your are referencing the sys_property.
You cannot utilize the sys_property value/string in exactly same method as you would a regex, because it is a plain old string not. Also your regex is incorrect and doesn't validate when tested externally
- I used https://regex101.com/ to validate it
Maybe try a sys_property value of
'([A-Z]{3})+([-]{1})+([0-9]{4})+([-]{1})+([0-9]{7})'
and consume it like
var toMatch = "WWW-0000-0000000";
var myAnswer = false;
var pattern = new RegExp(gs.getProperty('match.pattern.1'));
if(pattern.test(toMatch)){
myAnswer = true;
}
gs.info('myAnswer ' + myAnswer);
This works for me in a PDI
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2022 12:33 AM
thank you @Tony Chatfield1 this code works like a charm.