Estoy tratando de analizar algo de html de un sitio web.
El html puede contener algunos html no válidos que hacen que el analizador no pueda analizar el html.
esta es mi expresión regular que escribí
/(\[class\]((=)("|')?.*("|')))|(\[class\])|((\[id\]((=)("|')?.*("|')))|(\[id\]))/Esto eliminará todos los atributos [clase] e [id]
Mi expresión regular anterior funciona bien con algunos html pero no con todos los ejemplos 1 que funcionan
<div class="par fontsize-16" [class]="'par fontsize-' + fontsize"><p>the two of them left that everyone came back to their senses.</p>pero no funciona con
</div><span id="saved" hidden>Settings saved..</span><div class="clear"></div><div class="par fontsize-16" [class]="'par fontsize-' + fontsize"><p>It wasn't " until the two of them left that everyone came back to their senses.</p> Esto es causado por la cadena It wasn't " que se elimina.
Solo quiero eliminar el attr y su contenido y no el contenido de las etiquetas.
Es posible
Gracias a It Goldman terminé con una solución. Lo publico por si alguien lo necesita.
cleanHTML(html: string, ...attrs: string[]) { attrs.forEach(attr => { var pos = 0 while ((pos = html.indexOf(attr)) > -1) { var sep = null; var state = 0; if (html[pos + attr.length] === "=") { for (var i = pos + attr.length; i < html.length; i++) { var c = html.charAt(i); if (c == '=') { state = 1 continue; } if (state == 1 && (c.trim() === '"' || c.trim() === "'")) { console.log(c.trim()) sep = c; break; } else if (state === 1) break; } } if (!sep) { html = html.substring(0, pos) + html.substring(pos + attr.length + (state== 1 ? 1 : 0)); continue; } var pos_q = html.indexOf(sep, pos); var pos_q2 = html.indexOf(sep, pos_q + 1); html = html.substring(0, pos) + html.substring(pos_q2 + 1) } }); return html; } var src = `</div><span [class] [class][class] id="saved" [id]hidden>Settings saved..</span><div class="clear"></div><div class="par fontsize-16" [class]="'par fontsize-' + fontsize"><p>It wasn't " until the two of them left that everyone came back to their senses.</p><a [class]='another'>sasportas</a>` function clean_str(src, attributes_to_remove) { attributes_to_remove.forEach(function(attr) { var pos while ((pos = src.indexOf(attr)) > -1) { var sep; var state = 0; for (var i = pos + attr.length; i < src.length; i++) { var c = src.charAt(i); if (c == '=') { state = 1 continue; } if (state == 0 && c.trim()) { sep = null; break; } if (state == 1 && c.trim()) { sep = c; break; } } if (sep) { var pos_q = src.indexOf(sep, pos); var pos_q2 = src.indexOf(sep, pos_q + 1); src = src.substring(0, pos) + src.substring(pos_q2 + 1) } else { src = src.substring(0, pos) + src.substring(pos + attr.length) } } }) return src; } console.log(clean_str(src, ["[class]", "[id]"]))