Skip to content

Instantly share code, notes, and snippets.

@adriannier
Last active September 7, 2022 18:32
Show Gist options
  • Save adriannier/744507625aa9b7e6383df653d7db7ec6 to your computer and use it in GitHub Desktop.
Save adriannier/744507625aa9b7e6383df653d7db7ec6 to your computer and use it in GitHub Desktop.
var testStrings = ['']
// Valid email addresses
testStrings.push('[email protected]')
testStrings.push('[email protected]')
testStrings.push('[email protected]')
testStrings.push('[email protected]')
testStrings.push('[email protected]')
testStrings.push('[email protected] (may go to [email protected] inbox depending on mail server)')
testStrings.push('[email protected] (one-letter local-part)')
testStrings.push('[email protected]')
testStrings.push('test/[email protected] (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('[email protected] (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('[email protected] (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%[email protected] (% escaped mail route to [email protected] via example.org)')
testStrings.push('[email protected] (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@[email protected] (only one @ is allowed outside quotation marks)')
testStrings.push('Invalid: a"b(c)d,e:f;g<h>i[j\k][email protected] (none of the special characters in this local-part are allowed outside quotation marks)')
testStrings.push('Invalid: just"not"[email protected] (quoted strings must be dot separated or the only element making up the local-part)')
testStrings.push('Invalid: this is"not\[email protected] (spaces, quotes, and backslashes may only exist when within quoted strings and preceded by a backslash)')
testStrings.push('Invalid: this\ still\"not\\[email protected] (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('[email protected]')
testStrings.push('mailto:[email protected]')
testStrings.push('As Df <[email protected]>')
testStrings.push('[email protected],[email protected]')
testStrings.push('mailto:[email protected],mailto:[email protected]')
testStrings.push('As Df <[email protected]>, Qw Er <[email protected]>')
testStrings.push("\t\n E-Mail:\r\t As Df <[email protected]> \n\t\[email protected]\n\t")
testStrings.push("\t\n E-Mail:\r\t [email protected] \n\t\[email protected]")
testStrings.push("\t\n\[email protected]> \n\t\[email protected]")
testStrings.push("\t\n\r<[email protected] [email protected]")
testStrings.push("\t\n [email protected]")
testStrings.push("\t\n [email protected][email protected]")
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: "[email protected]"
Output: "[email protected]"
-----------------------------
Test 3
Input: "[email protected]"
Output: "[email protected]"
-----------------------------
Test 4
Input: "[email protected]"
Output: "[email protected]"
-----------------------------
Test 5
Input: "[email protected]"
Output: "[email protected]"
-----------------------------
Test 6
Input: "[email protected]"
Output: "[email protected]"
-----------------------------
Test 7
Input: "[email protected] (may go to [email protected] inbox depending on mail server)"
Output: "[email protected]"
-----------------------------
Test 8
Input: "[email protected] (one-letter local-part)"
Output: "[email protected]"
-----------------------------
Test 9
Input: "[email protected]"
Output: "[email protected]"
-----------------------------
Test 10
Input: "test/[email protected] (slashes are a printable character, and allowed)"
Output: "test/[email protected]"
-----------------------------
Test 11
Input: "admin@mailserver1 (local domain name with no TLD, although ICANN highly discourages dotless email addresses[10])"
Output: ""
-----------------------------
Test 12
Input: "[email protected] (see the List of Internet top-level domains)"
Output: "[email protected]"
-----------------------------
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: "[email protected] (bangified host route used for uucp mailers)"
Output: "[email protected]"
-----------------------------
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%[email protected] (% escaped mail route to [email protected] via example.org)"
Output: "user%[email protected]"
-----------------------------
Test 18
Input: "[email protected] (local part ending with non-alphanumeric character from the list of allowed printable characters)"
Output: "[email protected]"
-----------------------------
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@[email protected] (only one @ is allowed outside quotation marks)"
Output: "[email protected]"
-----------------------------
Test 23
Input: "Invalid: a"b(c)d,e:f;g<h>i[jk][email protected] (none of the special characters in this local-part are allowed outside quotation marks)"
Output: "[email protected]"
-----------------------------
Test 24
Input: "Invalid: just"not"[email protected] (quoted strings must be dot separated or the only element making up the local-part)"
Output: "[email protected]"
-----------------------------
Test 25
Input: "Invalid: this is"[email protected] (spaces, quotes, and backslashes may only exist when within quoted strings and preceded by a backslash)"
Output: "[email protected]"
-----------------------------
Test 26
Input: "Invalid: this still"not\[email protected] (even if escaped (preceded by a backslash), spaces, quotes, and backslashes must still be contained by quotes)"
Output: "[email protected]"
-----------------------------
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: "[email protected]"
Output: "[email protected]"
-----------------------------
Test 31
Input: "mailto:[email protected]"
Output: "[email protected]"
-----------------------------
Test 32
Input: "As Df <[email protected]>"
Output: "[email protected]"
-----------------------------
Test 33
Input: "[email protected],[email protected]"
Output: "[email protected]"
-----------------------------
Test 34
Input: "mailto:[email protected],mailto:[email protected]"
Output: "[email protected]"
-----------------------------
Test 35
Input: "As Df <[email protected]>, Qw Er <[email protected]>"
Output: "[email protected]"
-----------------------------
Test 36
Input: "
E-Mail:
As Df <[email protected]>
[email protected]
"
Output: "[email protected]"
-----------------------------
Test 37
Input: "
E-Mail:
[email protected]
[email protected]"
Output: "[email protected]"
-----------------------------
Test 38
Input: "
[email protected]>
[email protected]"
Output: "[email protected]"
-----------------------------
Test 39
Input: "
<[email protected] [email protected]"
Output: "[email protected]"
-----------------------------
Test 40
Input: "
[email protected]"
Output: "[email protected]"
-----------------------------
Test 41
Input: "
[email protected][email protected]"
Output: "[email protected]"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment