how to replace all occurrence of string between two symbols?

后端 未结 2 1170
时光说笑
时光说笑 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 first test. You might expect the regex to match and when continuing after that match, .

    But it does not. The regex will match first. Obviously not what we wanted.

提交回复
热议问题