const regex = /(?:(?<!\G)“|\G(?<!^|“))[^”]*?\K(?:\b(Volde(?:-?\s*)mort)\b|”(*SKIP)(*F))/gmi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?:(?<!\\G)“|\\G(?<!^|“))[^”]*?\\K(?:\\b(Volde(?:-?\\s*)mort)\\b|”(*SKIP)(*F))', 'gmi')
const str = `Voldemort has identified an opportunity to create an eighth horcrux. “How can Voldemort cast a Voldemort spell better than a Ron spell?” We can “run” the Voldemort test for Voldemort. “Simply put, Volde-
mort will be doing Voldemort things better than any non- Voldemort actor could,” Ron chanted. But only Voldemort knows where to find the “Voldemort” key to the Voldemort house.`;
// 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