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
03-31-2022 03:04 PM
Try this:
var regExpGr = new GlideRecord('{your table name}')
.where('{your id field}', '{value of field to lookup')
.select('{your regex field}')
.get();
var regExp = new RegExp(regExpGr.{your regex field}.toString());
if (regExp.text({string})) {
{do something}
}
Might have to tinker with the RegExp part, but should be a good starting point.
Aoife

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 03:37 PM
Hi Frank12,
ServiceNow has regex table OOTB. The name of the table is "question_regex" and it can be accessed from "Service Catalog" > "Catalog Variables" > "Variable Validation Regex".
Script Include:
var RegexUtil = Class.create();
RegexUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
test: function() {
var regexName = this.getParameter('sysparm_regex_name');
var testValue = this.getParameter('sysparm_value');
var gr = new GlideRecord('question_regex');
if (gr.get('name', regexName)) {
var regex = new RegExp(gr.regex);
return (regex.test(testValue).toString());
}
},
type: 'RegexUtil'
});
Client Script to test script include:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('RegexUtil');
ajax.addParam('sysparm_name', 'test');
ajax.addParam('sysparm_regex_name', g_form.getValue('regex_name'));
ajax.addParam('sysparm_value', newValue);
ajax.getXMLAnswer(function(answer) {
alert(answer);
if (answer.length > 0) {
g_form.setValue('result', answer);
}
});
}
Execution result:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2022 05:47 AM
I always thought that the question_reqex table table was only used for Variables and not actual fields on tables which is why i never thought to use it. Ill give this a shot and come back later with results. Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 11:53 AM
Thanks for the example, I was able to obtain a true/false value back which allowed me to adjust my client script with the return value. In my case was to clear out the value if false, it does show the regex validation message briefly but I created a show message with more details on why it was cleared.