considera esta cadena:
the quick <input type="button" disabled="" value="brown.fox" /> jumps over the <input type="button" disabled="" value="lazy.dog" /> Me gustaría reemplazar cada aparición de la etiqueta <input type="button" con una cadena que contenga el atributo de valor de la etiqueta, específicamente con esta cadena ${}
Así que el resultado final debería ser
the quick ${brown.fox} jumps over the ${lazy.dog}Como está en JavaScript, tiene un analizador DOM al alcance de su mano. ¡Úsalo!
const input = `the quick <input type="button" disabled="" value="brown.fox" /> jumps over the <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;let text = 'the quick <input type="button" disabled="" value="brown.fox" /> jumps over the <input type="button" disabled="" value="lazy.dog" />'; text = text.replace(/<input[^>]*value\s*=\s*["'](.+?)["']\s*[^>]*>/g,"${$1}")Necesitas usar expresiones regulares para esto.
Este código debería funcionar:
a = 'the quick <input type="button" disabled="" value="brown.fox" /> jumps over the <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)