12

I have a string that contains HTML image elements that is stored in a var.

I want to remove the image elements from the string.

I have tried: var content = content.replace(/<img.+>/,"");

and: var content = content.find("img").remove(); but had no luck.

Can anyone help me out at all?

Thanks

MeltingDog
  • 12,714
  • 38
  • 143
  • 270

8 Answers8

36
var content = content.replace(/<img[^>]*>/g,"");

[^>]* means any number of characters other than >. If you use .+ instead, if there are multiple tags the replace operation removes them all at once, including any content between them. Operations are greedy by default, meaning they use the largest possible valid match.

/g at the end means replace all occurrences (by default, it only removes the first occurrence).

Matt Coughlin
  • 17,988
  • 3
  • 44
  • 57
9
$('<p>').html(content).find('img').remove().end().html()
Scott Evernden
  • 37,365
  • 14
  • 77
  • 84
7

The following Regex should do the trick:

var content = content.replace(/<img[^>"']*((("[^"]*")|('[^']*'))[^"'>]*)*>/g,"");

It first matches the <img. Then [^>"']* matches any character except for >, " and ' any number of times. Then (("[^"]*")|('[^']*')) matches two " with any character in between (except " itself, which is this part [^"]*) or the same thing, but with two ' characters.

An example of this would be "asf<>!('" or 'akl>"<?'.

This is again followed by any character except for >, " and ' any number of times. The Regex concludes when it finds a > outside a set of single or double quotes.

This would then account for having > characters inside attribute strings, as pointed out by @Derek 朕會功夫 and would therefore match and remove all four image tags in the following test scenario:

<img src="blah.png" title=">:(" alt=">:)" /> Some text between <img src="blah.png" title="<img" /> More text between <img /><img src='asdf>' title="sf>">

This is of course inspired by @Matt Coughlin's answer.

Community
  • 1
  • 1
mlunoe
  • 4,384
  • 4
  • 32
  • 42
6

Use the text() function, it will remove all HTML tags!

var content = $("<p>"+content+"</p>").text();
powtac
  • 39,317
  • 26
  • 112
  • 166
2

I'm in IE right now...this worked great, but my tags come out in upper case (after using innerHTML, i think) ... so I added "i" to make it case insensitive. Now Chrome and IE are happy.

var content = content.replace(/<img[^>]*>/gi,"");
cakidnyc
  • 340
  • 4
  • 12
0

Does this work for you?:

var content = content.replace(/<img[^>]*>/g, '')
senfo
  • 27,730
  • 15
  • 74
  • 102
0

You could load the text as a DOM element, then use jQuery to find all images and remove them. I generally try to treat XML (html in this case) as XML and not try to parse through the strings.

var element = $('<p>My paragraph has images like this <img src="foo"/> and this <img src="bar"/></p>');

element.find('img').remove();

newText = element.html();

console.log(newText);
Jason
  • 2,611
  • 27
  • 28
0

To do this without regex or libraries (read jQuery), you could use DOMParser to parse your string, then use plain JS to do any manipulations and re-serialize to get back your string.

pulsejet
  • 1,054
  • 12
  • 26