I'm looking for a way to use regular expressions in [addQuery]

__17
Tera Expert

After looking at some articles, I thought I could use regular expressions to filter records in the following ways:

However, the expected result was not obtained.
Is there a way to filter records using regular expressions?

The target field is a string type.

 

 

var tmp_rec = new GlideRecord("table_name");
tmp_rec.query();
tmp_rec.next();


var reg = new RegExp(tmp_rec.getDisplayValue("Regular_expression_keywords"));//Regular expression string is set in "Regular_expression_keywords"


var target_rec = new GlideRecord("target_table_name");

target_rec.addQuery(message_field,reg);//"Message_field" is a string type
target_rec.query();

var rec_count = 0; 
rec_count = target_rec.getRowCount();
1 ACCEPTED SOLUTION

If you are trying to use a regex dynamically in a GlideRecord query, then I do not think this is possible unless you use GlideFilter within a 'while.next()' loop.

GlideFilter and Regular Expression Match - Developer Community - Blog - ServiceNow Community

If you are trying to use 'contains' in a Gliderecord query, then you need to use the syntax
addQuery('field_name', 'CONTAINS', 'yourValue');

The link I provided in my first reply contains the operators available and syntax

View solution in original post

6 REPLIES 6

Tony Chatfield1
Kilo Patron

Hi, what is the resulting value and data type for var reg?
'addQuery' requires a string input, and as you have it defined without any operator the query will only return results that are an exact match to 'reg'.

GlideRecord | ServiceNow Developers

The data is a string type.
Try the value as "\ D {3}".
The value will be changed dynamically by the user.

I want to filter the message part of a record with a regular expression.

If you are trying to use a regex dynamically in a GlideRecord query, then I do not think this is possible unless you use GlideFilter within a 'while.next()' loop.

GlideFilter and Regular Expression Match - Developer Community - Blog - ServiceNow Community

If you are trying to use 'contains' in a Gliderecord query, then you need to use the syntax
addQuery('field_name', 'CONTAINS', 'yourValue');

The link I provided in my first reply contains the operators available and syntax

It seems that it can be solved.
Thank you very much