Returning a string RegEx to a client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 12:04 PM
Hi all,
I have a custom table I built where I want to store regular expressions so that i can call them in client script/business rule. I am storing them in a string field.
Email RegEx Example: /[a-zA-Z0-9_+&*-]+(?:\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,7}/
I want to be able to use a client script/business rule to go into the table and grab the above RegEx and use it the following script:
if ({call from script include}.test({randome field})) {
//Do Something
}
But the value I am returning back from the script include is not correct. I think its because when return the value from my custom table its typeof == String and i need it to be something else.If i just do the below code it will work fine:
var reEmail = /[a-zA-Z0-9_+&*-]+(?:\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,7}/;
if (reEmail.test(newValue)) {
//Do Something
}
What am I missing? When I return the value from the script include I am logging the correct RegEx but its of a string type which is what I am thinking is incorrect. how can i return just the regEx of /[a-zA-Z0-9_+&*-]+(?:\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,7}/ without it being a string? Or if its something else, let me know. Thanks.
P.S: If you are wondering why im doing this its because if i ever want to go back and change the Regular Express I can easily go into the table and change it and i dont have to go into the script to do so. Any script changes need to go through Change which is a 2 week process and table changes dont need that.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2025 11:56 PM
I tried this in background script and it gave me error
var testValue = 'abc@gmail.com';
var gra = new GlideRecord('question_regex');
if (gra.get('name', 'Email')) {
var regex = new RegExp(gra.regex);
gs.print(regex.test(testValue).toString());
}
the error was
Script execution error: Script Identifier: null.null.script, Error Description: Invalid quantifier ?, Script ES Level: 0
Evaluator: com.glide.script.RhinoEcmaError: Invalid quantifier ?
script : Line(4) column(0)
1: var testValue = 'abc@gmail.com';
2: var gra = new GlideRecord('question_regex');
3: if (gra.get('name', 'Email')) {
==> 4: var regex = new RegExp(gra.regex);
5: gs.print(regex.test(testValue).toString());
6: }