I\'m working with RegEx on Javascript and here is where I stuck.
I have a simple string like
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 likeThis 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.
/(<.*?>)/
Just use this. Replace all the occurrences with ""
.
See demo.