Regular Expression to remove Div tags

This question already has an answer here:

  • RegEx match open tags except XHTML self-contained tags 35 answers

  • 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.


    Found the solution:

    replace(/<div[^>]*?>[sS]*?</div>/gi, "")
    

    Please refer to: I'm looking for a regular expression to remove a given (x)HTML tag from a string

    链接地址: http://www.djcxy.com/p/76854.html

    上一篇: 将HTML转换为有效的XML标签

    下一篇: 正则表达式去除Div标签