const regex = new RegExp('(?im)^(?<c1>\\"?)\\w+(?:[\\W&&[^@]]?)\\w+\\k<c1>@(?:\\[?(?:\\d{3}\\.?){0,4}\\]?|(?:[\\w&&[^\\d]]+[\\.\\-]?)*(?<=\\.\\w{2,6}))$', 'g')
const str = `List of Valid Email Addresses
email@example.com
firstname.lastname@example.com
email@subdomain.example.com
firstname+lastname@example.com
email@123.123.123.123
email@[123.123.123.123]
"email"@example.com
1234567890@example.com
email@example-one.com
_______@example.com
email@example.name
email@example.museum
email@example.co.jp
firstname-lastname@example.com
List of Strange Valid Email Addresses
much.”more\\ unusual”@example.com
very.unusual.”@”.unusual.com@example.com
very.”(),:;<>[]”.VERY.”very@\\\\ "very”.unusual@strange.example.com
List of Invalid Email Addresses
plainaddress
#@%^%#\$@#\$@#.com
@example.com
Joe Smith <email@example.com>
email.example.com
email@example@example.com
.email@example.com
email.@example.com
email..email@example.com
あいうえお@example.com
email@example.com (Joe Smith)
email@example
email@-example.com
email@example.web
email@111.222.333.44444
email@example..com
Abc..123@example.com
List of Strange Invalid Email Addresses
”(),:;<>[\\]@example.com
just”not”right@example.com
this\\ is"really"not\\allowed@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