how to replace all occurrence of string between two symbols?

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

I have a simple string like

<html><body><span style=3D"font-family:Verdana; color:#000; font-size:10pt;=
"><div><font face=3D"verdana, geneva" size=3D"2">,120</body></html>

all i need to do is write javascript which can replace all strings in with "<" and ">" symbol.

I wrote something like this -

var strReplaceAll = Body;
var intIndexOfMatch = strReplaceAll.indexOf( "<" );

while (intIndexOfMatch != -1){

    strReplaceAll = strReplaceAll.replace(/<.*>/,'')

    intIndexOfMatch = strReplaceAll.indexOf( "<" );

but the problem is if body contains -


it will give me -


only or if body contains like -


it will give me nothing please let me know how i can get-


as a final output.



Try this regex instead:




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.


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

See demo.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.