views:

322

answers:

4

Hi,

I have a div tag, nested with many span and div tags inside it.

now i want a regular expression in javascript which will strip the div and the content inside it.

thanks in advance..

A: 

This is not possible using only regular expressions. Using something like jquery is much easier.

Thexa4
This is not possible using regular expressions *only*.
Gumbo
+2  A: 

Regular expressions can't handle nesting, at least JavaScript regexes can't (and those that can, like .NET and PCRE, aren't easy to handle).

This could only work if there is just one outermost <div> tag - then the regular expression

/<div>.*<\/div>/s` 

will match everything from the very first <div> to the very last </div> in your document.

Tim Pietzcker
+6  A: 

You want to remove a <div> element from your document?

First things first; learn the DOM!

var aReferenceToMyDiv = document.getElementById('foo');
aReferenceToMyDiv.parentNode.removeChild(aReferenceToMyDiv);

... will remove the <div> element when applied to the following DOM structure:

<div id="foo">
    <span>...</span>
    other stuff...
</div>
J-P
A: 

Hi,

found solution

replace(/]?>[\s\S]?<\/div>/gi, "")

ref : http://stackoverflow.com/questions/116403/im-looking-for-a-regular-expression-to-remove-a-given-xhtml-tag-from-a-string

Thanks..

BABA
Have you even bothered reading the answers offered, or perhaps taken a critical view of the regex solution?
J-P