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 + "}
If you can describe the token syntax inside ${...} you can still use a regex, too.
If we assume that
${+, -, * or / operators"...\"...\\...")}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)