0

Text:

god is a <span class="linkText0" name="name1">good</span> god my <span class="linkText1" name="name2">friend</span>

Expected (remove <span>..</span> given the name matches)

god is a good god my <span class="linkText1" name="name2">friend</span>

My Regex:

/<span[^]+name=\"name2\">([^]+)</span>/g

Results:

god is a friend

http://regexr.com?36fbh

Please help to correct

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
Amarsh
  • 11,214
  • 18
  • 53
  • 78
  • 3
    Please don't use regex to parse (or manipulate) HTML. The
    cannot hold. Zalgo is Tony the Pony, he comes! http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454
    – Anthony Accioly Sep 25 '13 at 03:21
  • Your regex and your expectation doesn't match...Your expectation removes span named "name1", yet your regex looks for "name2". If you're looking for "name1": http://regexr.com?36fbk **BUT** again, you shouldn't use RegExp to manipulate HTML. – Passerby Sep 25 '13 at 03:31
  • It should be `/]+name=\"name2\">([^<>]+)/g` ... **WARNING** Not Responsible For any Damages caused by using regex to parse html – Anirudha Sep 25 '13 at 03:33
  • Thanks @Anirudh . I have a situation where i am forming my own dom, hence no chances of failure. Please put your comment as an answer so that I can mark it correct. – Amarsh Sep 25 '13 at 03:48
  • If you are creating your own HTML, then why don't create the HTML without the offending tags? – Andy Lester Sep 25 '13 at 03:55
  • What language are you using? And What is your expected result text? – hwnd Sep 25 '13 at 03:57

1 Answers1

0

Use this regex

/<span[^<>]+name=\"name2\">([^<>]+)</span>/g

[^<>] would match anything except < or >

Though you should use an html parser.

Anirudha
  • 32,393
  • 7
  • 68
  • 89