const regex = /(?i)(?:\w+\W+){0,9}?(?=.*(?:plot).*|.*(?:over).*)(?:\w+\W+){0,9}?(?=Charlie|Muir|Charlie Muir)\K(?:(?:Charlie|Muir|Charlie Muir)\s+)+/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?i)(?:\\w+\\W+){0,9}?(?=.*(?:plot).*|.*(?:over).*)(?:\\w+\\W+){0,9}?(?=Charlie|Muir|Charlie Muir)\\K(?:(?:Charlie|Muir|Charlie Muir)\\s+)+', 'gm')
const str = `
cHarlie muir charlie charlie was over the moon the the the the the tge the the the
-the the the the the the the the the the the the the the the the the-
over was the the the moon parker parker charlie muir charlie
-the the the the the the the the the the the the the the the the the-
Charlie muir charlie was near the plot
-the the the the the the the the the the the the the the the the the-
ChArlie was far away from the plot was far away from the
-the the the the the the the the the the the the the the the the the-
the plot was far away from CharLie
-the the the the the the the the the the the the the the the the the-
the plot was far away from CharLie muir charlie
-the the the the the the the the the the the the the the the the the-
the over was far away from CHarlie muir
-the the the the the the the the the the the the the the the the the-
Mr cHARLIE was bowled over test was bowled after the ball
-the the the the the the the the the the the the the the the the the-
over the test was bowled muIR
-the the the the the the the the the the the the the the the the the-
the plot the ball the slot it will plot the test was bowled MuiR
-the the the the the the the the the the the the the the the the the-
`;
// 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