Why's my RegExp.test locking up?

Adobe Community Professional ,
Aug 01, 2022 Aug 01, 2022

Copy link to clipboard

Copied

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. 

 

TOPICS
Scripting

Views

134

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Adobe Community Professional , Aug 01, 2022 Aug 01, 2022

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]);

- Mark

Likes

Translate

Translate
Adobe Community Professional , Aug 01, 2022 Aug 01, 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

Likes

Translate

Translate
Adobe Community Professional ,
Aug 01, 2022 Aug 01, 2022

Copy link to clipboard

Copied

Hi @brianp311,

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 01, 2022 Aug 01, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 01, 2022 Aug 01, 2022

Copy link to clipboard

Copied

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]);

- Mark

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 01, 2022 Aug 01, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

Hi @brianp311 ,

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/...

 

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 )

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

LATEST

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

-Manan

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines