Getting something weird with regex and sys_properties

naxis alx
Tera Contributor

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

1 ACCEPTED SOLUTION

Tony Chatfield1
Kilo Patron

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

View solution in original post

3 REPLIES 3

AnubhavRitolia
Mega Sage
Mega Sage

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})$/;

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Tony Chatfield1
Kilo Patron

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

thank you @Tony Chatfield1 this code works like a charm.