Christopher M. answered 08/04/22
Experienced JavaScript Developer
Hi Brendan,
The first thing I'd do to trouble shoot this is actually just take the JavaScript code out and run it separate from InDesign, which you can see run here: https://replit.com/@cmcculloh/MintcreamOrdinaryPreprocessor#index.js
This code outputs the following:
This not only will let you see that there is nothing wrong with your Regex/loop (as far as JS is concerned), but also you can see an edge-case/bug you might not have considered.
It appears to me that this is actually an Indesign specific issue and not actually an issue with JS/Regex.
Where I'd recommend going from here would be to add some sort of logging inside of the loop. Print out each "str" as soon as you get it from str = openDocument.layers.item(i).name; and see if you are seeing what you are expecting? Log the value for "LayerLength" before you run the loop, is it maybe only one item long?