Skip to main content
dublove
Legend
Jun 21, 2025 4:50 PM UTC
Answered

Can I use variables in regular? Or is it just wrong thinking?

For example, a character in my regular is determined by using a certain character on the filename.

It may or may not be @.
But that doesn't mean you can use @* for either case.

 

It's not an assumption, it's that I have to determine which case to use based on whether the filename has the @ symbol or not.

 

afterTheAtSymbol = /\@(.*)(\.[^\.]+)$/,
afterTheAtSymbol = /\^(.*)(\.[^\.]+)$/,

 

 

Manan JoshiCorrect answer
Community Expert
Jun 23, 2025 7:46 AM UTC

Hi Manan Joshi

Another question, I'm searching for something between [ ] in a filename.
If match[1] is returned,
I want to determine if the match[1] I found is @,
I originally wrote it this way, but it says @ is a reserved character and is forbidden to be used this way.
if (match[1] === '@')
Pop-up error: @ Reserved characters, Tip. Prohibition of use

What to do?

more info


I don't get this error. See my test code below

var abc = "filename[@]hello.txt"
var regexp = new RegExp(".*\\[(.*)\\]")
var match = regexp.exec(abc)
if (match[1] == "@")
    alert("Hello")

It alerts hello for me and no error

-Manan

-Manan

6 replies

Community Expert
Jun 23, 2025 2:54 AM UTC

What do you mean by regular? I hope regular expression. Your posts are too cryptic one has to guess in order to understand what you are trying to say. Do you proofread before posting? Even if English is not your first language you can use the translate methods to see if it makes sense or not

Anyhow if the question is, can you use variables while constructing a regular expression, then the answer is yes. You can create an object of RegExp class and that takes a string as an argument to the constructor.

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#RegExp.html

You can find lots of examples on how to use this object on the internet and this forum as well

-Manan

Translate
-Manan
dublove
dubloveAuthor
Legend
Jun 23, 2025 3:40 AM UTC

Yes, it's when a character may be a variable in a regular expression.
For example this:
@ needs to be taken from the current script filename. It is not deterministic. 

afterTheAtSymbol = /\@(.*)(\.[^\.]+)$/,

 

Translate
Community Expert
Jun 23, 2025 3:47 AM UTC

Something like the following would work

var temp = "\\@"
var regexp = new RegExp(temp + "(.*)(\\.[^\\.]+)$")

-Manan

Translate
-Manan