Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7655803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:36:27+00:00 2026-05-31T12:36:27+00:00

Long time user, first time asker; very new to regular expressions. I’m a designer

  • 0

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 🙂

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.

var openDocument = app.documents.item(0);
var LayerLength = openDocument.layers.length;

wordToMatch = "print";

for (i=0;i<LayerLength;i++)
{
    myRegEx = new RegExp(wordToMatch,"i");

    str = openDocument.layers.item(i).name;
    if (str.search(myRegEx) >= 0)
    {
        openDocument.layers.item(i).visible = true;
    }
}

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.

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:

wordToMatch = "print";
myRegEx = new RegExp(wordToMatch,"i");

for (i=0;i<LayerLength;i++)
{
    str = openDocument.layers.item(i).name;

    if (str.search(myRegEx) >= 0)
    {
        openDocument.layers.item(i).visible = true;
    }
}

But this only does what it’s supposed to do on the first layer, then it’s unable to match any subsequent layers.

Why is this? I feel like I’m misunderstanding something fundamental here, and I’d like to know what that is.

Thanks,
Brendan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:36:28+00:00Added an answer on May 31, 2026 at 12:36 pm

    A regular expression has a lastIndex property-
    when a match is found, last index points at the next character after the match,
    resetting to 0 only after all the matches are found.

    This is so you can find the next match in the same string-
    since you are only looking for the first match, reset the lastIndex yourself in the loop.

    for(i= 0; i<LayerLength; i++){
        myRegEx.lastIndex= 0;
        str= openDocument.layers.item(i).name;
        if(str.search(myRegEx)>= 0){
            openDocument.layers.item(i).visible= true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

long time reader first time user. I'm putting together my first RoR application and
(Long time reader of SO, first time asking a q. I'm quite new to
Long time mysql/php user, first time Drupal user. Actually, I'm not doing the Drupal
The very first time you start a push app, the user gets asked if
I am a first time user of the new JPA 2.0 Criteria API and
long-time listener, first-time caller! I’m trying to connect to MySQL using the ADO.NET Entity
After what seems like a very long time trying to find a solution, I
First question from a long time user. I'm writing a Perl script that will
I am a long-time, generally very happy user of Prototype. I recently switched to
Long time user, first time poster. I've found similar questions/answers, typically involving subqueries, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.