Skip to main content
dublove
Legend
June 21, 2025
Answered

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

  • June 21, 2025
  • 1 reply
  • 317 views

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 = /\^(.*)(\.[^\.]+)$/,

 

 

Correct answer Manan Joshi

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

1 reply

Community Expert
June 23, 2025

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

-Manan
dublove
dubloveAuthor
Legend
June 23, 2025

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 = /\@(.*)(\.[^\.]+)$/,

 

Community Expert
June 23, 2025

Something like the following would work

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

-Manan

-Manan