how to replace all occurrence of string between two symbols?

只谈情不闲聊 提交于 2019-12-02 10:20:00

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.

vks
/(<.*?>)/

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

See demo.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!