1

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">http://72.55.146.142:8880/=
order003.png.zip,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 -

test<abc>test2<adg>

it will give me -

test

only or if body contains like -

<html>test<abc>test2<adg>

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

testtest2

as a final output.

georg
  • 211,518
  • 52
  • 313
  • 390
Aamir
  • 738
  • 2
  • 17
  • 41

2 Answers2

2

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.

Community
  • 1
  • 1
Stephan
  • 41,764
  • 65
  • 238
  • 329
  • While this works, it's a hack based on the lack of `">"` in the HTML tags. Parsing HTML means you'll never get it right - see ``. – Unihedron Sep 01 '14 at 13:37
1
/(<.*?>)/

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

See demo.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
vks
  • 67,027
  • 10
  • 91
  • 124