🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #9859

How to use variables in RegExps

#9859
From: Sue_Klefstad
Date: Thu, 13 Jul 2006 19:29
14 lines
1224 bytes
I figured out how to incorporate a variable into a regular expression and thought I'd share the joy:

It's a two-step process of first building a string that includes the variable and then constructing a RegExp from that string with any flags that are needed. For example, I need to search globally for figure callouts (such as Figure 12.1 or figure 2-3) where the first number is the same as the current chapter number, which is in the variable myChapNum:

myRegExpString = "[fF]igure " + myChapNum + "[.-][0-9]+";
myRegExp = new RegExp(myRegExpString, "g");

So that if myChapNum holds the number 2 then my RegExp becomes

/[fF]igure 2[.-][0-9]+/g

It works!

-- Sue
Technical Details
Message-ID: <3bc0e7e8.-1@webcrossing.la2eafNXanI>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news.glorb.com!news.ecp.fr!news.stben.net!webcrossing

Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,55416a17b5cb36a4,start
X-Google-Attributes: gidf939a,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: Sue_Klefstad@adobeforums.com
Newsgroups: adobe.scripting.indesign
Subject: How to use variables in RegExps
Message-ID: <3bc0e7e8.-1@webcrossing.la2eafNXanI>
Date: Thu, 13 Jul 2006 12:29:47 -0700
Lines: 19
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news.glorb.com!news.ecp.fr!news.stben.net!webcrossing