const regex = /(?im)^(?=.{1,64}@)(?:("[^"\\]*(?:\\.[^"\\]*)*"@)|((?:(?:\([^)]*\)|[^\W_])(?:\.(?!\.)|[-!#\$%&'\*\+\/=\?\^`\{\}\|~\w]|\([^)]*\))*)?[^\W_](?:\([^)]*\))?@))(?=.{1,255}$)(?:(\[[^\[\]]+\])|((?:(?=.{1,63}\.)[^\W_][-\w]*[^\W_]*\.)+[^\W_](?:[^\W_]|-){0,22}[^\W_])|((?=.{1,63}$)[^\W_][-\w]*))$/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?im)^(?=.{1,64}@)(?:("[^"\\\\]*(?:\\\\.[^"\\\\]*)*"@)|((?:(?:\\([^)]*\\)|[^\\W_])(?:\\.(?!\\.)|[-!#\\$%&\'\\*\\+\\\/=\\?\\^`\\{\\}\\|~\\w]|\\([^)]*\\))*)?[^\\W_](?:\\([^)]*\\))?@))(?=.{1,255}$)(?:(\\[[^\\[\\]]+\\])|((?:(?=.{1,63}\\.)[^\\W_][-\\w]*[^\\W_]*\\.)+[^\\W_](?:[^\\W_]|-){0,22}[^\\W_])|((?=.{1,63}$)[^\\W_][-\\w]*))$', 'g')
const str = `info@kamikaze.uk
alfred@live.nl
dubai@organon.it
karel.de.koning@hetnet.nl
decock@upc.nl
jiazhe_xu@sina.cn
xfunkblasterx@yahoomail.org
super-duper-email@hotmail.com
"John..Doe"@example.com
john.smith(comment)@example.com
(comment)john.smith@example.com
john.smith@worldwideweb.123com_org
"John."(),:;<>@[\\].Doe"@.123radio_tv
"much.more unusual"@example.com
other.email-with-dash@example.com
disposable.style.email.with+symbol@example.com
"()<>[]:,;@\\\\"!#\$%&'-/=?^_\`{}| ~.a"@example.org
smith@[IPv6:2001:db8::1]
jsmith@[192.168.2.1]`;
// 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