Last active
September 7, 2022 18:32
-
-
Save adriannier/744507625aa9b7e6383df653d7db7ec6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var testStrings = [''] | |
// Valid email addresses | |
testStrings.push('simple@example.com') | |
testStrings.push('very.common@example.com') | |
testStrings.push('disposable.style.email.with+symbol@example.com') | |
testStrings.push('other.email-with-hyphen@example.com') | |
testStrings.push('fully-qualified-domain@example.com') | |
testStrings.push('user.name+tag+sorting@example.com (may go to user.name@example.com inbox depending on mail server)') | |
testStrings.push('x@example.com (one-letter local-part)') | |
testStrings.push('example-indeed@strange-example.com') | |
testStrings.push('test/test@test.com (slashes are a printable character, and allowed)') | |
testStrings.push('admin@mailserver1 (local domain name with no TLD, although ICANN highly discourages dotless email addresses[10])') | |
testStrings.push('example@s.example (see the List of Internet top-level domains)') | |
testStrings.push('" "@example.org (space between the quotes)') | |
testStrings.push('"john..doe"@example.org (quoted double dot)') | |
testStrings.push('mailhost!username@example.org (bangified host route used for uucp mailers)') | |
testStrings.push('"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com (include non-letters character AND multiple at sign, the first one being double quoted)') | |
testStrings.push('user%example.com@example.org (% escaped mail route to user@example.com via example.org)') | |
testStrings.push('user-@example.org (local part ending with non-alphanumeric character from the list of allowed printable characters)') | |
testStrings.push('postmaster@[123.123.123.123] (IP addresses are allowed instead of domains when in square brackets, but strongly discouraged)') | |
testStrings.push('postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334] (IPv6 uses a different syntax)') | |
// Invalid email addresses | |
testStrings.push('Invalid: Abc.example.com (no @ character)') | |
testStrings.push('Invalid: A@b@c@example.com (only one @ is allowed outside quotation marks)') | |
testStrings.push('Invalid: a"b(c)d,e:f;g<h>i[j\k]l@example.com (none of the special characters in this local-part are allowed outside quotation marks)') | |
testStrings.push('Invalid: just"not"right@example.com (quoted strings must be dot separated or the only element making up the local-part)') | |
testStrings.push('Invalid: this is"not\allowed@example.com (spaces, quotes, and backslashes may only exist when within quoted strings and preceded by a backslash)') | |
testStrings.push('Invalid: this\ still\"not\\allowed@example.com (even if escaped (preceded by a backslash), spaces, quotes, and backslashes must still be contained by quotes)') | |
testStrings.push('Invalid: 1234567890123456789012345678901234567890123456789012345678901234+x@example.com (local-part is longer than 64 characters)') | |
testStrings.push('Invalid: i_like_underscore@but_its_not_allowed_in_this_part.example.com (Underscore is not allowed in domain part)') | |
testStrings.push('Invalid: QA[icon]CHOCOLATE[icon]@test.com (icon characters)') | |
// Custom tests | |
testStrings.push('asdf@asdf.de') | |
testStrings.push('mailto:asdf@asdf.de') | |
testStrings.push('As Df <asdf@asdf.de>') | |
testStrings.push('asdf@asdf.de,qwer@qwer.de') | |
testStrings.push('mailto:asdf@asdf.de,mailto:qwer@qwer.de') | |
testStrings.push('As Df <asdf@asdf.de>, Qw Er <qwer@qwer.de>') | |
testStrings.push("\t\n E-Mail:\r\t As Df <asdf@asdf.de> \n\t\rqwer@qwer.de\n\t") | |
testStrings.push("\t\n E-Mail:\r\t asdf@asdf.de \n\t\rqwer@qwer.de") | |
testStrings.push("\t\n\rasdf@asdf.de> \n\t\rqwer@qwer.de") | |
testStrings.push("\t\n\r<asdf@asdf.de qwer@qwer.de") | |
testStrings.push("\t\n asdf+0123456789@asdf.de") | |
testStrings.push("\t\n asdf+0123456789@asdf.de+qwer@qwer.de") | |
for (var i = 0; i < testStrings.length; i++) { | |
console.log("-----------------------------\nTest " + (i + 1) + "\nInput: \"" + testStrings[i] + "\"\nOutput: \"" + extractEmail(testStrings[i]) + "\"\n\n") | |
} | |
function extractEmail(text) { | |
const matches = text.match(/(?:[a-z0-9+!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/gi) | |
if (matches && matches.length > 0) { | |
return matches[0] | |
} | |
return "" | |
} | |
/* | |
----------------------------- | |
Test 1 | |
Input: "" | |
Output: "" | |
----------------------------- | |
Test 2 | |
Input: "simple@example.com" | |
Output: "simple@example.com" | |
----------------------------- | |
Test 3 | |
Input: "very.common@example.com" | |
Output: "very.common@example.com" | |
----------------------------- | |
Test 4 | |
Input: "disposable.style.email.with+symbol@example.com" | |
Output: "disposable.style.email.with+symbol@example.com" | |
----------------------------- | |
Test 5 | |
Input: "other.email-with-hyphen@example.com" | |
Output: "other.email-with-hyphen@example.com" | |
----------------------------- | |
Test 6 | |
Input: "fully-qualified-domain@example.com" | |
Output: "fully-qualified-domain@example.com" | |
----------------------------- | |
Test 7 | |
Input: "user.name+tag+sorting@example.com (may go to user.name@example.com inbox depending on mail server)" | |
Output: "user.name+tag+sorting@example.com" | |
----------------------------- | |
Test 8 | |
Input: "x@example.com (one-letter local-part)" | |
Output: "x@example.com" | |
----------------------------- | |
Test 9 | |
Input: "example-indeed@strange-example.com" | |
Output: "example-indeed@strange-example.com" | |
----------------------------- | |
Test 10 | |
Input: "test/test@test.com (slashes are a printable character, and allowed)" | |
Output: "test/test@test.com" | |
----------------------------- | |
Test 11 | |
Input: "admin@mailserver1 (local domain name with no TLD, although ICANN highly discourages dotless email addresses[10])" | |
Output: "" | |
----------------------------- | |
Test 12 | |
Input: "example@s.example (see the List of Internet top-level domains)" | |
Output: "example@s.example" | |
----------------------------- | |
Test 13 | |
Input: "" "@example.org (space between the quotes)" | |
Output: "" | |
----------------------------- | |
Test 14 | |
Input: ""john..doe"@example.org (quoted double dot)" | |
Output: ""john..doe"@example.org" | |
----------------------------- | |
Test 15 | |
Input: "mailhost!username@example.org (bangified host route used for uucp mailers)" | |
Output: "mailhost!username@example.org" | |
----------------------------- | |
Test 16 | |
Input: ""very.(),:;<>[]".VERY."very@\ "very".unusual"@strange.example.com (include non-letters character AND multiple at sign, the first one being double quoted)" | |
Output: "".unusual"@strange.example.com" | |
----------------------------- | |
Test 17 | |
Input: "user%example.com@example.org (% escaped mail route to user@example.com via example.org)" | |
Output: "user%example.com@example.org" | |
----------------------------- | |
Test 18 | |
Input: "user-@example.org (local part ending with non-alphanumeric character from the list of allowed printable characters)" | |
Output: "user-@example.org" | |
----------------------------- | |
Test 19 | |
Input: "postmaster@[123.123.123.123] (IP addresses are allowed instead of domains when in square brackets, but strongly discouraged)" | |
Output: "postmaster@[123.123.123.123]" | |
----------------------------- | |
Test 20 | |
Input: "postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334] (IPv6 uses a different syntax)" | |
Output: "" | |
----------------------------- | |
Test 21 | |
Input: "Invalid: Abc.example.com (no @ character)" | |
Output: "" | |
----------------------------- | |
Test 22 | |
Input: "Invalid: A@b@c@example.com (only one @ is allowed outside quotation marks)" | |
Output: "c@example.com" | |
----------------------------- | |
Test 23 | |
Input: "Invalid: a"b(c)d,e:f;g<h>i[jk]l@example.com (none of the special characters in this local-part are allowed outside quotation marks)" | |
Output: "l@example.com" | |
----------------------------- | |
Test 24 | |
Input: "Invalid: just"not"right@example.com (quoted strings must be dot separated or the only element making up the local-part)" | |
Output: "right@example.com" | |
----------------------------- | |
Test 25 | |
Input: "Invalid: this is"notallowed@example.com (spaces, quotes, and backslashes may only exist when within quoted strings and preceded by a backslash)" | |
Output: "notallowed@example.com" | |
----------------------------- | |
Test 26 | |
Input: "Invalid: this still"not\allowed@example.com (even if escaped (preceded by a backslash), spaces, quotes, and backslashes must still be contained by quotes)" | |
Output: "allowed@example.com" | |
----------------------------- | |
Test 27 | |
Input: "Invalid: 1234567890123456789012345678901234567890123456789012345678901234+x@example.com (local-part is longer than 64 characters)" | |
Output: "1234567890123456789012345678901234567890123456789012345678901234+x@example.com" | |
----------------------------- | |
Test 28 | |
Input: "Invalid: i_like_underscore@but_its_not_allowed_in_this_part.example.com (Underscore is not allowed in domain part)" | |
Output: "" | |
----------------------------- | |
Test 29 | |
Input: "Invalid: QA[icon]CHOCOLATE[icon]@test.com (icon characters)" | |
Output: "" | |
----------------------------- | |
Test 30 | |
Input: "asdf@asdf.de" | |
Output: "asdf@asdf.de" | |
----------------------------- | |
Test 31 | |
Input: "mailto:asdf@asdf.de" | |
Output: "asdf@asdf.de" | |
----------------------------- | |
Test 32 | |
Input: "As Df <asdf@asdf.de>" | |
Output: "asdf@asdf.de" | |
----------------------------- | |
Test 33 | |
Input: "asdf@asdf.de,qwer@qwer.de" | |
Output: "asdf@asdf.de" | |
----------------------------- | |
Test 34 | |
Input: "mailto:asdf@asdf.de,mailto:qwer@qwer.de" | |
Output: "asdf@asdf.de" | |
----------------------------- | |
Test 35 | |
Input: "As Df <asdf@asdf.de>, Qw Er <qwer@qwer.de>" | |
Output: "asdf@asdf.de" | |
----------------------------- | |
Test 36 | |
Input: " | |
E-Mail: | |
As Df <asdf@asdf.de> | |
qwer@qwer.de | |
" | |
Output: "asdf@asdf.de" | |
----------------------------- | |
Test 37 | |
Input: " | |
E-Mail: | |
asdf@asdf.de | |
qwer@qwer.de" | |
Output: "asdf@asdf.de" | |
----------------------------- | |
Test 38 | |
Input: " | |
asdf@asdf.de> | |
qwer@qwer.de" | |
Output: "asdf@asdf.de" | |
----------------------------- | |
Test 39 | |
Input: " | |
<asdf@asdf.de qwer@qwer.de" | |
Output: "asdf@asdf.de" | |
----------------------------- | |
Test 40 | |
Input: " | |
asdf+0123456789@asdf.de" | |
Output: "asdf+0123456789@asdf.de" | |
----------------------------- | |
Test 41 | |
Input: " | |
asdf+0123456789@asdf.de+qwer@qwer.de" | |
Output: "asdf+0123456789@asdf.de" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment