0

I am manipulating HTML string after that it may possible that string contain some empty ( no text ) elements ( it can be many level) eg. <div><div><span></span></div></div> I want to remove these tags using regular expression. currently I am using DOM manipulation to remove these elements.

Anoop
  • 23,044
  • 10
  • 62
  • 76
  • 4
    What's wrong with your DOM implementation? Also, you have an unclosed `` tag. Good luck parsing it with regex ;) – Blender Nov 14 '11 at 17:55
  • 2
    Parsing html with regex is generally a bad idea. http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html – James Montagne Nov 14 '11 at 17:55
  • The following HTML you provided is invalid as one of the span tags is not closed – John Hartsock Nov 14 '11 at 17:56
  • I think you should consider looking why these tags are on the page in the first place – Ibu Nov 14 '11 at 17:57
  • 2
    @James: could've just linked to the original SO post: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 it's much more impressive with the actual unicode art. – Marc B Nov 14 '11 at 17:58
  • I removed extra span and I am removing some text using string manipulation( JavaScript ). – Anoop Nov 14 '11 at 18:01
  • @MarcB You know, I've seen that post a hundred times and never really wondered what all of those boxes at the end were... even more impressive now that I've seen it with the unicode rendered properly. – James Montagne Nov 14 '11 at 18:27

2 Answers2

1

Not what you are looking for but just an idea: it may be enough to replace some strings like:

<div></div>
<span></span>

with empty strings. way simpler than using regex since usually there arent as many html tags that you want to remove.

Alex
  • 474
  • 3
  • 10
0

If you don't want your DOM implementation and want to use regex, I'm assuming it might be because you want to do this outside of JavaScript.

If so, XSLT may be an option.

Matthew
  • 8,183
  • 10
  • 37
  • 65
  • I want to use JavaScript. I am doing this to make my application fast . Dom manipulation is very slow. – Anoop Nov 15 '11 at 05:37