how to replace all occurrence of string between two symbols?

后端 未结 2 1171
时光说笑
时光说笑 2021-01-24 12:57

I\'m working with RegEx on Javascript and here is where I stuck.

I have a simple string like



        
相关标签:
2条回答
  • 2021-01-24 13:43

    Try this regex instead:

    <[^>]+>
    

    DEMO:

    http://regex101.com/r/kI5cJ7/2

    DISCUSSION

    Put the html code in a string and apply to this string the regex.

    var htmlCode = ...;
    htmlCode = htmlCode.replace(/<[^>]+>/g, '');
    

    The original regex take too much characters (* is a greedy operator).

    Check this page about Repetition with Star and Plus, especially the part on "Watch Out for The Greediness!".

    Most people new to regular expressions will attempt to use <.+>. They will be surprised when they test it on a string like This is a <EM>first</EM> test. You might expect the regex to match <EM> and when continuing after that match, </EM>.

    But it does not. The regex will match <EM>first</EM>. Obviously not what we wanted.

    0 讨论(0)
  • 2021-01-24 13:52
    /(<.*?>)/
    

    Just use this. Replace all the occurrences with "".

    See demo.

    0 讨论(0)
提交回复
热议问题