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:
I've tried stripping out the slash marks and apostrophes before testing with:
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 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
Hi @brianp311 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]);
Thank you both. Substituting brackets solved the issue. A curious case indeed.
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));
Must be an ExtendScript bug. Its regex engine has several of them.
Hi @brianp311 ,
also see into this old thread from 2011:
Regular Expressions in CS5.5 - something is wrong
inspiria_si, Nov 03, 2011
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.
( Adobe Community Professional )