Is it possible to store regex in a field and then use it in code?
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2025 03:43 AM
I've a custom table in which I can create a new field. The purpose of this field will be to store regex expression.
Then in the code I wish to use the following:
var toMatch = "someValue";
var myAnswer = false;
var gr = new GlideRecord('my_custom_table');
if(gr.get('xxxxxxxxx'))
{
var pattern = new RegExp(gr.getValue('regex_column_name'));
if(pattern.test(toMatch)){
myAnswer = true;
}
gs.info('myAnswer ' + myAnswer);
}
I tried setting the field type to "String" but that doesn't seem to work. Is there a specific "column type" that I can try?
10 REPLIES 10
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2025 03:51 AM - edited 05-27-2025 03:53 AM
Hi @Abhijit4
Tried your approach. Doesn't seem to be working.
Ran this in the background script
gs.print(new test_foo().checkRegDecode('abc@gmail.com'));
And got the following output
Here is the checkRegDecode function
checkRegDecode: function(str){
var gr = new GlideRecord('u_bar_demo');
gr.get('1c1839568711eed03065c8870cbb355d');
var reg = gr.getValue('u_regex');
gs.log(reg);
gs.log(str);
var pattern = new RegExp(`${reg}`);
if(pattern.test(str)){
return true;
}
return false;
}
And here how it is stored in the table