Is it possible to store regex in a field and then use it in code?

Nisar3
Giga Guru

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

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

Nisar3_0-1748342974821.png

 

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

Nisar3_2-1748343225112.png

 

Nisar3_1-1748343098901.png