开发者

Regular Expression to remove Div tags [duplicate]

This question already has answers here: RegEx match open tags except XHTML self-contained tags (35 answers) Closed 5 years ago.

I have a div tag, nested within many span 开发者_开发知识库and div tags.

Now I want a regular expression in JavaScript which will strip the div tags and get the content inside it.


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜