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 =
var content = content.replace(/
"']*((("[^"]*")|('[^']*'))[^"'>]*)*>/g,"");
It first matches the . 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:
Some text between
More text between ![]()
This is of course inspired by @Matt Coughlin's answer.