Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove image elements from string

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

like image 551
MeltingDog Avatar asked Jun 14 '12 00:06

MeltingDog


2 Answers

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).

like image 99
Matt Coughlin Avatar answered Sep 20 '22 07:09

Matt Coughlin


$('<p>').html(content).find('img').remove().end().html()
like image 25
Scott Evernden Avatar answered Sep 20 '22 07:09

Scott Evernden