regex inconsistent

JulianP32954866
Tera Expert

Trying to check against some values in sys_attachment and i am getting very odd results.

In this case I am checking the file name ends in xls or xlsx.  Pretty simple, but my code was on validating again 2 of 3 records.  So I took the code out and ran it.
I have tried a few things such as getting the file name into a variable and checking, clearing the variable between the loop.

In the end I ran the regex.test again the variable twice.....

var regex = /(.xls|.xlsx)$/gim;

var strName;
var gr = new GlideRecord('sys_attachment');
gr.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^table_sys_id=a339b4ca83a9b210b9e29bc6feaad360')
gr.query();
while(gr.next()) {
   strName = gr.getValue('file_name') +'';
  gs.info(gr.getValue('sys_id') + ' : ' + regex.test(strName) + ' : ' +'"'+strName +'"');
  gs.info(gr.getValue('sys_id') + ' : ' + regex.test(strName) + ' : ' +'"'+strName +'"');
  
  strName = null
}

 

 My results are

2025-12-02 16:30:16 7279b4ca83a9b210b9e29bc6feaad34d : true : "skills matrix results.xlsx"
2025-12-02 16:30:16 7279b4ca83a9b210b9e29bc6feaad34d : false : "skills matrix results.xlsx"
2025-12-02 16:30:16 208930ca83a9b210b9e29bc6feaad3e9 : true : "sort callouts.xlsx"
2025-12-02 16:30:16 208930ca83a9b210b9e29bc6feaad3e9 : false : "sort callouts.xlsx"
2025-12-02 16:30:16 e47930ca83a9b210b9e29bc6feaad3e6 : true : "sort callouts.xlsx"
2025-12-02 16:30:16 e47930ca83a9b210b9e29bc6feaad3e6 : false : "sort callouts.xlsx"

 Why does it seem to make a difference ?

I did note that if I set the regex during the loop it works, but my problem is that for the code I am using I am passing the regex through so not really going able to use that method

 

Anyone got any ideas ?

 

1 ACCEPTED SOLUTION

removing the g from the flag works

View solution in original post

3 REPLIES 3

JulianP32954866
Tera Expert

Thought I'd just call a function to do the regex check for me - out of the loop, being passed the values, should work - yes ?  No 😞

function checkRE(regex, strValue){
	return regex.test(strValue);
}


var regex = /(.xls|.xlsx)$/gim;

var strName;
var gr = new GlideRecord('sys_attachment');
gr.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^table_sys_id=a339b4ca83a9b210b9e29bc6feaad360')
gr.query();
while(gr.next()) {
   strName = gr.getValue('file_name') +'';
  // gs.info(gr.getValue('sys_id') + ' : ' + regex.test(strName) + ' : ' +'"'+strName +'"');
  // gs.info(gr.getValue('sys_id') + ' : ' + regex.test(strName) + ' : ' +'"'+strName +'"');
  gs.info(gr.getValue('sys_id') + ' : ' + checkRE(regex, strName) + ' : ' +'"'+strName +'"');
 	
  
  strName = null
}

and I am back to my original issue that the 2nd check is false when it should be true

2025-12-02 16:38:43 7279b4ca83a9b210b9e29bc6feaad34d : true : "skills matrix results.xlsx"
2025-12-02 16:38:43 208930ca83a9b210b9e29bc6feaad3e9 : false : "sort callouts.xlsx"
2025-12-02 16:38:43 e47930ca83a9b210b9e29bc6feaad3e6 : true : "sort callouts.xlsx"

As with the original post, adding the gs.info line twice

  gs.info(gr.getValue('sys_id') + ' : ' + checkRE(regex, strName) + ' : ' +'"'+strName +'"');
  gs.info(gr.getValue('sys_id') + ' : ' + checkRE(regex, strName) + ' : ' +'"'+strName +'"');
 	

and I get the same as before

2025-12-02 16:40:15 7279b4ca83a9b210b9e29bc6feaad34d : true : "skills matrix results.xlsx"
2025-12-02 16:40:15 7279b4ca83a9b210b9e29bc6feaad34d : false : "skills matrix results.xlsx"
2025-12-02 16:40:15 208930ca83a9b210b9e29bc6feaad3e9 : true : "sort callouts.xlsx"
2025-12-02 16:40:15 208930ca83a9b210b9e29bc6feaad3e9 : false : "sort callouts.xlsx"
2025-12-02 16:40:15 e47930ca83a9b210b9e29bc6feaad3e6 : true : "sort callouts.xlsx"
2025-12-02 16:40:15 e47930ca83a9b210b9e29bc6feaad3e6 : false : "sort callouts.xlsx"

 

removing the g from the flag works

@JulianP32954866 Try below:

 

function checkRE(regex, strValue) {
return regex.test(strValue);
}
var regex1 = '(.xls|.xlsx)$';

var strName;
var gr = new GlideRecord('sys_attachment');
gr.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^table_sys_id=a339b4ca83a9b210b9e29bc6feaad360');
gr.query();
while (gr.next()) {
var rg = new RegExp(regex1, 'jgim');
strName = gr.getValue('file_name');
gs.info(gr.getValue('sys_id') + ' : ' + checkRE(rg, strName) + ' : ' + '"' + strName + '"');
}

Raghav
MVP 2023
LinkedIn