0

How could I replace or remove all the image tag in my string using javascript?

Coffee Bean<div><br /></div><img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/4gxYSUNDX.." />
I want to remove the image and the successive image tag in a string. How to do it?
  • 4
    Possible duplicate of [How to replace all occurrences of a string in JavaScript?](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – ab29007 Sep 14 '17 at 10:51
  • 3
    [You can't parse HTML with regex.](https://stackoverflow.com/a/1732454/402322) – ceving Sep 14 '17 at 10:59

1 Answers1

9

Using Regex:

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

AncientYouth
  • 471
  • 1
  • 4
  • 12