user3014202
user3014202

Reputation: 99

Regex to strip img and iframe tags

.replace(/<img[^>]+>/g, '')

this regex strip img tags only how can we modify this so that it strips iframe tags also along with img tags

The result regex must strip img and iframe tags only strictly. It must not strip anchor link p span blockquote etc

Upvotes: 1

Views: 2182

Answers (1)

erosman
erosman

Reputation: 7721

There are instances that RegEx can be used to strip tags from strings such as responseText of XMLHttpRequest

For such instances you can try

.replace(/<img[^>]+>(<\/img>)?|<iframe.+?<\/iframe>/g, '')

Explanation:

<img[^>]+> // anythign between the <img ... >
(<\/img>)? // in case it is old style HTML
<iframe.+?<\/iframe> // anything between <iframe....</iframe> non-greedy

If the target is a live DOM, then use DOM to remove elements ie Node.removeChild

Good luck :)

Upvotes: 2

Related Questions