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

120
Visualizações
Find and replace expression in a string with HTML tags

This is a follow up question of Replace HTML tag with string that contains tag attribute.

This time I would like to do the opposite process and replace occurrences of ${something.foo.bar} back to <input type="button" disabled="" data-value="something.foo.bar" value="foo.bar" />. For example consider this string:

"the quick ${something.brown.fox} jumps over the ${something.lazy.dog}"

The end result should be:

the quick <input type="button" disabled="" data-value="something.brown.fox" value="brown.fox" /> jumps over the <input type="button" disabled="" data-value="something.lazy.dog" value="lazy.dog" />

Note that something. is a known prefix and will always be present inside the curly braces, so the input data-value should be the value between the braces and the input value attribute should contain the value without the prefix.

I've tried to solve it with multiple chaining of Regex match and replaceAll but didn't managed to get the desired outcome.

Help appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can make it simply using String.replace:

const s = "the quick ${something.brown.fox} jumps over the ${something.lazy.dog}"
const res = s.replace(/\$\{([^}]+)\}/g, (x, y) => {
  const [prefix, ...rest] = y.split('.')
  return `<input type="button" disabled="" data-value="${y}" value="${rest.join('.')}" />`
})

console.log(res)

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