Note that there are some explanatory texts on larger screens.

plurals
  1. PORegex defined outside of for loop not working
    primarykey
    data
    text
    <p>Long time user, first time asker; very new to regular expressions. I'm a designer trying to make my life in InDesign easier, so please be patient if this is an easy one :)</p> <p>I'm working on a script that pulls pages out of a master file into a template file. Some of these master pages have layers that are to be used when the final file is exported to PDF for print; other layers are to be used when the final file is exported to e-mailing. So, if someone checks an option to save for e-mailing, I want the print layers to hide and the e-mail layers to show. Easy enough, but I wanted to streamline the code as a function so I could specify "print" or "email" as a variable, and then the script would match that to any variable that has "print" in it. Regex territory.</p> <pre><code>var openDocument = app.documents.item(0); var LayerLength = openDocument.layers.length; wordToMatch = "print"; for (i=0;i&lt;LayerLength;i++) { myRegEx = new RegExp(wordToMatch,"i"); str = openDocument.layers.item(i).name; if (str.search(myRegEx) &gt;= 0) { openDocument.layers.item(i).visible = true; } } </code></pre> <p>So, this actually works. It does what I want it to do (Haven't put it in a function yet but I'll get there). It finds layers with "print" in them and then makes them visible.</p> <p>This didn't seem ideal to me, though. I thought it would make more sense to define the regex once and then use it multiple times in the for loop, as follows:</p> <pre><code>wordToMatch = "print"; myRegEx = new RegExp(wordToMatch,"i"); for (i=0;i&lt;LayerLength;i++) { str = openDocument.layers.item(i).name; if (str.search(myRegEx) &gt;= 0) { openDocument.layers.item(i).visible = true; } } </code></pre> <p>But this only does what it's supposed to do on the first layer, then it's unable to match any subsequent layers.</p> <p>Why is this? I feel like I'm misunderstanding something fundamental here, and I'd like to know what that is. </p> <p>Thanks, Brendan</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload