The following regex should do the trick:
var content = content.replace(/<img[^>"']*((("[^"]*")|('[^']*'))[^"'>]*)*>/g,"");
First it matches <img
. Then [^>"']*
any number of characters is executed except >
, "
and '
. Then (("[^"]*")|('[^']*'))
matches the two "
with any character between them (except "
which is this part [^"]*
) or the same, but with two characters '
.
An example of this would be "asf<>!('"
Or 'akl>"<?'
.
This is followed again by any character except >
, "
and '
as many times as necessary. Regex terminates when it finds >
outside a set of single or double quotes.
Then it would take into account the presence of >
characters inside the attribute strings, as @Derek 朕 會 功夫 indicated , and therefore would 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, of course, is inspired by @Matt Coughlin 's answer.
source share