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

93
Vistas
Replace HTML tag with string that contains tag attribute

consider this string:

the quick&nbsp;<input type="button" disabled="" value="brown.fox" />&nbsp;jumps over the&nbsp;<input type="button" disabled="" value="lazy.dog" />

I would like to replace every occurrence of the <input type="button" tag with a string that contains the value attribute of the tag, specifically with this string ${}

So the end result should be

the quick&nbsp;${brown.fox}&nbsp;jumps over the&nbsp;${lazy.dog}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

As you are in JavaScript, you have a DOM parser at your fingertips. Use it!

const input = `the quick&nbsp;<input type="button" disabled="" value="brown.fox" />&nbsp;jumps over the&nbsp;<input type="button" disabled="" value="lazy.dog" />`;
const container = document.createElement('div');
container.innerHTML = input;
const buttons = container.querySelectorAll("input[type=button]");
buttons.forEach(button=>{
  button.replaceWith("${"+button.value+"}");
});
const output = container.innerHTML;
about 4 years ago · Juan Pablo Isaza Denunciar

0

let text = 'the quick&nbsp;<input type="button" disabled="" value="brown.fox" />&nbsp;jumps over the&nbsp;<input type="button" disabled="" value="lazy.dog" />';

text = text.replace(/<input[^>]*value\s*=\s*["'](.+?)["']\s*[^>]*>/g,"${$1}")
about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to use regex for this.

This code should work:

a = 'the quick&nbsp;<input type="button" disabled="" value="brown.fox" />&nbsp;jumps over the&nbsp;<input type="button" disabled="" value="lazy.dog" />'

pattern = /<input type=\"button\".*?value=\"([^\"]+)\" \/>/gm

matches = a.matchAll(pattern);
    
for (const match of matches) {
  a = a.replace(match[0], "${" + match[1] + "}")
}

console.log(a)

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