Regular expression to remove div tags

I have a div tag nested in many span and div tags.

Now I want a regular expression in JavaScript that will separate the div tags and get the contents inside it.

+3
source share
3 answers

Do you want to remove an item <div>from your document?

First things first; find out the DOM!

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

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

<div id="foo">
    <span>...</span>
    other stuff...
</div>
+6
source

Regular expressions cannot handle nesting, at least JavaScript regexes cannot (and those that can, for example, .NET and PCRE, are not easy to process).

, <div> -

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

<div> </div> .

+2

Found a solution:

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

Refer to: I am looking for a regex to remove a given (x) HTML tag from a string

+1
source

Source: https://habr.com/ru/post/1745265/


All Articles