You can use a temporary element to clean that.
I usually do like this:
function limparHTML(html) {
const proxy = document.createElement('div');
proxy.innerHTML = html;
return proxy.innerText;
}
const HTML = 'Vestibulum varius lectus a ante euismod <img src="teste2.jpg"> cursus. Nam sed semper augue, a laoreet purus. <img src="teste.jpg"> Vivamus ut risus eu lectus imperdiet sollicitudin.';
const limpo = limparHTML(HTML);
alert(limpo);
If you are in Node.js environment or cannot use elements as I suggested, you can try with Regexp.
In this case it could be so:
function limparHTML(html) {
return html.replace(/<img[^>]*>/g, '');
}
const HTML = 'Vestibulum varius lectus a ante euismod <img src="teste2.jpg"> cursus. Nam sed semper augue, a laoreet purus. <img src="teste.jpg"> Vivamus ut risus eu lectus imperdiet sollicitudin.';
const limpo = limparHTML(HTML);
alert(limpo);
The idea of /<img[^>]+>/g is:
- must have the string
<img
- after the string (top)
[^>]* means "any character, excluding the > zero or more times"
- end with the character `>``
g means "globally" in all occurrences
Note: In the first version of my reply with regex had [^>]+, but I switched to *, zero or more times, and that Randrade first suggested, as there may be cases of tags only with <img>
Gosh, 1 sec... I’ll leave only yours that is more complete :p
– Randrade
@Randrade let it be, you deserve it
+1even because you use*which is better than+. And I’ll fix it to use it*– Sergio
All right then xD
– Randrade