Skip to main content
brian_p_dts
Community Expert
Community Expert
August 2, 2022
Answered

Why's my RegExp.test locking up?

  • August 2, 2022
  • 5 replies
  • 725 views

Hi all, 

I have the following Regular Expresion I'm running in InDesign to compare layer names to certain tagging. The script is freezing on this string: 

var sizerx = /^(Y|W)?\d?X?(L|M|S|T)( |-)+?(Master)?$/g;
var str = "Women's 1/4 Zipper Guidelines - DO NOT TOUCH";
alert(sizerx.test(str));

I'm checking the layers to see if they match the following kinds of codes, all of which return true with the above regex: 

  • 3XS - Master
  • 2XS
  • XL Master
  • YM - Master
  • etc.

I've tried stripping out the slash marks and apostrophes before testing with: 

.replace(/[^a-zA-Z0-9\- \.]/gi,"");

To no avail. Is my regex too greedy? A more elegant way to write it? I will admit to regex not being my strong suit, but standard ECMA6 regex finders online successfully return false for the given str variable above. Thanks in advance. 

 

This topic has been closed for replies.
Correct answer Manan Joshi

As @m1b used [] instead of (), the following regex worked for me

 

/^[YW]?\d?X?[LMST][ |-]+?(Master)?$/g

 

But the point still remains to identify what is causing the hang.

-Manan

5 replies

Community Expert
August 3, 2022

Thanks @Marc Autret and @Laubender for confirming what we suspected.

-Manan

-Manan
Community Expert
August 2, 2022

Hi @brian_p_dts ,

also see into this old thread from 2011:

 

Regular Expressions in CS5.5 - something is wrong
inspiria_si, Nov 03, 2011
https://community.adobe.com/t5/indesign-discussions/regular-expressions-in-cs5-5-something-is-wrong/td-p/3742449

 

Quoting the OP:

The third example - how to freeze ID 5.5 in one line (I posted it earlier in Photoshop forum because similiar problem was called earlier):

alert((/(n|s)? /gmi).test('s') );

 

It still will freeze InDesign 2022 when e.g. run from the Scripts panel.

Just tested it with 17.3.0 on Windows 10.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

 

Marc Autret
Legend
August 2, 2022
brian_p_dts
Community Expert
Community Expert
August 2, 2022

Thank you both. Substituting brackets solved the issue. A curious case indeed. 

Community Expert
August 2, 2022

Ok, so I have arrived at a simplied regex exhibiting the issue. Seems like using ? with a group is somehow causing the regex engine to go into an endless loop. The following hangs as well. Let's hope @Peter Kahrel has some insights to share on this

 

var sizerx = /^(Y|W)?a/g;
var str = "Wo";
alert(sizerx.test(str));

 

 -Manan

-Manan
Peter Kahrel
Community Expert
Community Expert
August 2, 2022

Must be an ExtendScript bug. Its regex engine has several of them.

m1b
Community Expert
Community Expert
August 2, 2022

Hi @brian_p_dts how about this? A slightly different approach. I moved a bit of cleanup to a last minute .replace() before testing the regex.

var sizerx = /^(\d?[XY]?[LMST])(\sMaster)?/;
var tests = [
    '3XS - Master',
    '2XS',
    'XL Master',
    'YM - Master',
    'Women\'s 1/4 Zipper Guidelines - DO NOT TOUCH'
];

for (var i = 0; i < tests.length; i++)
    $.writeln(sizerx.test(tests[i].replace(/[ \-]+/g, ' ')) + ': ' + tests[i]);

- Mark

Community Expert
August 2, 2022

Hi @brian_p_dts,

This is interesting, if it test it on online regex evaluators I don't see any suspect backtracking or anything suspecting. But I confirm that ESTK does hang. I don't think this is a case of catastrophic backtracking, this is something else probably a ExtendScript issue

-Manan

-Manan
Manan JoshiCommunity ExpertCorrect answer
Community Expert
August 2, 2022

As @m1b used [] instead of (), the following regex worked for me

 

/^[YW]?\d?X?[LMST][ |-]+?(Master)?$/g

 

But the point still remains to identify what is causing the hang.

-Manan

-Manan