Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
Regular expression that finds all matches of ${any expression}

The following regular expression should find all matches of ${any expression}, the code is as follows:

const reg= /\$\{[^]+\}/g
let txt= '`${i + " test"} RESULT ${2 + 4} now ${i} hi`'
let result= [...txt.matchAll(reg)];
console.log(result)

As you will notice, the result is that it extracts almost the entire string, the correct operation should print in the console an array of 3 elements that would contain the ${any expression}

The following case shows an error that is generated if I use: [^}]

const reg= /\$\{[^}]+\}/g
let i= "some"
let txt= `${i + " test"} RESULT ${2 + 4} now ${i + "}" } hi`
let txtString= '`${i + " test"} RESULT ${2 + 4} now ${i + "}" } hi`'
let result= [...txtString.matchAll(reg)];
console.log(result)
console.log(txt)

the expression ${i + "}" } is valid in JavaScript so the regular expression should return [${i + "}" }, other matches] but in the example shown it returns

${i + "}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you can describe the token syntax inside ${...} you can still use a regex, too.

If we assume that

  • The match starts with ${
  • The tokens inside may be separated with zero or more whitespaces
  • The tokens are separated +, -, * or / operators
  • The tokens are either strings of word chars (letters, digits, underscores) or double quoted string literals ("...\"...\\...")
  • The match ends with }

You can then use

const reg= /\${\s*(?:\w+|"[^"\\]*(?:\\[^][^"\\]*)*")(?:\s*[-+\/*]\s*(?:\w+|"[^"\\]*(?:\\[^][^"\\]*)*"))*\s*}/g

See the regex demo.

See the JavaScript demo:

const token = '(?:\\w+|"[^"\\\\]*(?:\\\\[^][^"\\\\]*)*")';
const op = '[-+/*]';
const reg= new RegExp(String.raw`\${\s*${token}(?:\s*${op}\s*${token})*\s*}`, 'g');
let i= "some"
let txt= `${i + " test"} RESULT ${2 + 4} now ${i + "}" } hi`
let txtString= '`${i + " test"} RESULT ${2 + 4} now ${i + "}" } hi`'
let result= [...txtString.matchAll(reg)];
console.log(result)
console.log(txt)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!