const regex = /<(?<first>[^>]+)>.*?<\/(\k<first>)>/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('<(?<first>[^>]+)>.*?<\\\/(\\k<first>)>', 'g')
const str = `<p>normal</p><p><strong>gras</strong></p><p><em>italique</em></p><p><strong><em>gras italique</em></strong></p><p><s>barré</s></p><p><em><s>barré italique</s></em></p><p><strong><s>barré gras</s></strong></p><p><strong><em><s>barré gras italique</s></em></strong></p><p><u>souligné</u></p><p><em><u>souligné italique</u></em></p><p><strong><u>souligné gras</u></strong></p><p><strong><em><u>souligné gras italique</u></em></strong></p><p><s><u>souligné barré</u></s></p><p><em><s><u>souligné barré italique</u></s></em></p><p><strong><s><u>souligné gras barré</u></s></strong></p><p><strong><em><s><u>souligné gras barré italique</u></s></em></strong></p><p><a href="google.com" rel="noopener noreferrer" target="_blank">lien</a></p><p><br></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