How to remove img tags from text?

Asked

Viewed 213 times

7

I need to remove all img tags from javascript text. So the images don’t come.
Note: The text is random.

Like:

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.

How you should be:

Vestibulum varius lectus a ante euismod cursus. Nam sed semper augue, a laoreet purus. Vivamus ut risus eu lectus imperdiet sollicitudin.

2 answers

10


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 let it be, you deserve it +1 even because you use * which is better than +. And I’ll fix it to use it *

  • All right then xD

5

You can also use the following expression for this:

var textoConvertido = html.replace(/<img[^>]*>/g, '');

See the example below:

var 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.';
var textoConvertido = html.replace(/<img[^>]*>/g, '');

console.log(textoConvertido);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.