I am trying to find a regex that matches if all tags in a piece of HTML are empty, regardless of attributes and if there are nested tags.
My code essentially looks like this:
const emptyHtmlTagRegex = new RegExp(/<[^/>][^>]*></[^>]+>/);
if (emptyHtmlTagRegex.test(this.htmlContent)) {
// Output placeholder
};
And I’d like to get a match when this.htmlContent
is for example:
<ul><li class="class-name"></li></ul>
<p></p>
But not match if this.htmlContent
is:
<p>nomatch</p><ul><li></li><ul><p></p>
The regex does not match the entire string so the test method reports false positives if there are trailing empty tags: Regex 101 sample
I’ve also tried to use this.htmlContent.replace(regex,'')
and look for empty strings but that failed because of compatibility issues with the testing framework I use.