Skip to content

Instantly share code, notes, and snippets.

@manchuck
Created July 5, 2023 16:52
Show Gist options
  • Save manchuck/2233f0e460de49465d299c8fc1af49d2 to your computer and use it in GitHub Desktop.
Save manchuck/2233f0e460de49465d299c8fc1af49d2 to your computer and use it in GitHub Desktop.
email regex
const isEmail = (email) => {
const regex = /(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
return regex.test(email);
}
const validEmails = [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'test/[email protected]',
'admin@mailserver1',
'[email protected]',
'" "@example.org',
'"john..doe"@example.org',
'[email protected]',
'"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com',
'user%[email protected]',
'[email protected]',
'postmaster@[123.123.123.123]',
'postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]',
'Pelé@example.com',
'δοκιμή@παράδειγμα.δοκιμή',
'我買@屋企.香港',
'二ノ宮@黒川.日本',
'медведь@с-балалайкой.рф',
'संपर्क@डाटामेल.भारत',
]
validEmails.forEach((email) => {
console.log(`did ${email} appear as email? ${isEmail(email) ? 'yes' : 'no'}`)
})
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'test/[email protected]',
'admin@mailserver1',
'[email protected]',
'" "@example.org',
'"john..doe"@example.org',
'[email protected]',
'"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com',
'user%[email protected]',
'[email protected]',
'postmaster@[123.123.123.123]',
'postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]',
'Pelé@example.com',
'δοκιμή@παράδειγμα.δοκιμή',
'我買@屋企.香港',
'二ノ宮@黒川.日本',
'медведь@с-балалайкой.рф',
'संपर्क@डाटामेल.भारत'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment