const regex = new RegExp('(?<full_tag><\\s*+(?<name>[^\\s>]++)[^>]*+>(?>[^<>]++|(?&full_tag))*+</\\s*+\\g{name}\\s*+>)(*SKIP)(*F)|<\\s*+(?<bad_tag>[^\\s>]++)[^>]*+>[^<>]++', 'gm')
const str = `<p>hgfgff</p>
<p>ffdffd</h2>
<p>zsfdffd</h6>
<p>hgfgff</p>
<p>hgfgff<h1><b>text</b><u>text</u></h1></p>
`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions