Remove image elements from string

前端 未结 8 2279
独厮守ぢ
独厮守ぢ 2021-02-20 16:47

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 =

8条回答
  •  我在风中等你
    2021-02-20 17:37

    The following Regex should do the trick:

    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.

提交回复
热议问题