Home > Software engineering >  Regular expression that finds all matches of ${any expression}
Regular expression that finds all matches of ${any expression}

Time:05-18

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   "}

CodePudding user response:

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)

  • Related