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.