Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda