const regex = /the/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('the', 'g')
const str = `Text starts with (Regex Expression /)
This test is to help with the Regex when the symbol is / and looks for any text where the text is contained
Change the box above to have the word the and it will match all the thes in the text, this is case sensitive.
-------------
Text starts with (Regex Expression ^)
This test is to help with the Regex when the symbol is ^ and looks for text which starts with ^
Change the box above to have the word ^Text and it will match the first example.
-------------
Text ends with (Regex Expression \$)
This test is to help with the Regex when the symbol is \$ and looks for text which ends with xxxxx\$
Change the box above to have the word example\$ and make sure that you are looking at the multi-line add this in the text field at the top near the g (will not be relevant for URL) and it will match the last example
`;
// 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