How can I count the occurence of closing divs
$("body").html().match(/\/div/gi).length;
The Code above is not working as expected, although I escaped the slash. https://jsfiddle.net/zjqtpje4/1/
How can I count the occurence of closing divs
$("body").html().match(/\/div/gi).length;
The Code above is not working as expected, although I escaped the slash. https://jsfiddle.net/zjqtpje4/1/
Firstly, never use Regex to parse HTML.
Secondly, the number of closing div tags will always be equal to the number of opening div tags as the browser will automatically insert any mismatching tags.
You can see this behaviour in the console of the below snippet. Two closing </div>
will be added to the code:
console.log($("body").html().match(/\/div/gi).length);
console.log($('body').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<div>
<div></div>
The only way this could work is if you can work with the HTML as a string before it is placed in the DOM. This will prevent the HTML being corrected.