const regex = /(?=<[^<]*\s+title="([^"]*)")|<\s*a[^>]*>([^<]*)(?:[^\/]*[^a]*[^>]*)(?<=[>])([^<]*)/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?=<[^<]*\\s+title="([^"]*)")|<\\s*a[^>]*>([^<]*)(?:[^\\\/]*[^a]*[^>]*)(?<=[>])([^<]*)', 'g')
const str = `<p title="caca"></p>
<p> </p>
<b>
</b>
<B>title=" gdd "</B>
<i title="caca">dsfgjdgfs</i>
<i title=" boudin ">dsfgjdgfs</i>
<i> </i>
<i> </i>
<strong></strong>
<a>hello</a>
<i style="color: orange"></i>
<a href="#"> bizurot </a> !probleme
<o: class="ms-crap"></o>
<a href="#" title=" kameleo " > test </a> !probleme <a href="#"> cramoute </a> !probleme
<a href="#" title=" banane " > test <strong>hello</strong> asaidali </a><a href="#"> hdsfahsdgf </a> !probleme </br>
<p>Not empty</p>
<p>Not
empty</p>
<i>
</i>`;
// 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