const regex = /the/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('the', 'gm')
const str = `Here are some words: one, two, three, FOUR.
And here are some numbers: 1, 2, 3, 4, 10, 20, 30, 40, 100, 200, 300, 400, 1000.
Here are some mixed things: A1, A2, A3, A4
3 + 4 = 7
12 / 3 = 4
2 ^ 2 = 4
I think I've seen this film before
And I didn't like the ending
You're not my homeland anymore
So what am I defending now?
You were my town
Now I'm in exile, seein' you out
I think I've seen this film before
I can see you starin', honey
Like he's just your understudy
Like you'd get your knuckles bloody for me
Second, third, and hundredth chances
Balancin' on breaking branches
Those eyes add insult to injury`;
// 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