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 = [
'simple@example.com',
'very.common@example.com',
'disposable.style.email.with+symbol@example.com',
'other.email-with-hyphen@and.subdomains.example.com',
'fully-qualified-domain@example.com',
'user.name+tag+sorting@example.com',
'x@example.com',
'example-indeed@strange-example.com',
'test/test@test.com',
'admin@mailserver1',
'example@s.example',
'" "@example.org',
'"john..doe"@example.org',
'mailhost!username@example.org',
'"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com',
'user%example.com@example.org',
'user-@example.org',
'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'}`)
})
'simple@example.com',
'very.common@example.com',
'disposable.style.email.with+symbol@example.com',
'other.email-with-hyphen@and.subdomains.example.com',
'fully-qualified-domain@example.com',
'user.name+tag+sorting@example.com',
'x@example.com',
'example-indeed@strange-example.com',
'test/test@test.com',
'admin@mailserver1',
'example@s.example',
'" "@example.org',
'"john..doe"@example.org',
'mailhost!username@example.org',
'"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com',
'user%example.com@example.org',
'user-@example.org',
'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