- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2022 06:12 PM
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();
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2022 06:57 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2022 08:44 AM
Hi Tony, is this option more performant than just retrieving all the records and going through them one by one?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2022 09:31 PM
Hi 盆栽,
Correct answer has already been selected but for your information, it's possible to use regular expression in filter in GlideQuery.
e.g. from ServiceNow document.
var filteredQuery = new global.GlideQuery('sys_user')
.getBy({ sys_id: 'f682abf03710200044e0bfc8bcbe5d38' }, ['phone'])
.filter(function (user) {
return phoneRegex.test(user.phone);
});