const regex = new RegExp('(?P<localPart>[\\w.]+)@(?P<domain>[\\w]+)(\\.(?P<subDomain>[\\w]{2,10}))+(\\.(?P<TopLevelDomain>[\\w]{2,6}))?(?#Verify e-mail address validity: <local-part>@<domain>.<top-level-domain>)', 'gi')
const str = `hello3.why@gmail.co.uk
contact@regex101.com
hello3.2.why@gmail.co.uk
contact@regex101.companion thisone@gmail.com
hi@some.me.jp.sj.com
not valid@brokenemail.com
jon.apple@somewhere.co.dn.uk
localpartgoeshere@foo.example.com
`;
// 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