Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

250
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda