const regex = new RegExp('\\[[^\\]\\[]*\\](?=[^\\]\\[]*<em>)', 'gm')
const str = `[P1/1]0(4)0(5)**[P1/432]** g(5)I(2)d(7)a(8)\`<em>\`b(5)[P1/4]C(6)e(7)B(8)B\`</em>\`(9)[P1/5]0(6)i(7)[P1/6]0(1)I(2)[P1/7]0(6)[P1/1]0(1)0(2)[P1/2]E(1)c(2)d(3)a(4)**[P1/3]** 0(1)\`<em>\`b(2)[P1/4]C(1)e(2)B(3)B\`</em>\`(4)[P1/5]0(1)
`;
// 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