Created
March 21, 2018 16:53
-
-
Save mscalora/06173ef3e7595b003e6e5601343f098c to your computer and use it in GitHub Desktop.
Parse emails from text
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
parseEmails: function (emailStr) { | |
// remove RFC-822 adornments & normalize delimiters | |
var rfc822Cleaner = /(?:"[^@"'<>]*?"|(?:^|\n| |,|;)(?:[^@"'<>])+?)\s*?<([-a-z0-9_.%+]+?@[-a-z0-9.]+?.[-a-z0-9]{2,63}?)>/gi, | |
exchangeCleaner = /"(?:[^@"()])*?\(([-a-z0-9_.%+]+?@[-a-z0-9.]+?.[-a-z0-9]{2,63})\)(?:[^@"()])*?"\s*?<\1>/gi, | |
cleanedStr = emailStr.replace(rfc822Cleaner, ' $1 ').replace(exchangeCleaner, ' $1 ').replace(/(\n|[;, ])+/g, ';'), | |
emailList = _.split(cleanedStr, ';'); | |
var re = /(?:^|\s|\n|[;,<"'(])([-a-z0-9_.%+]+@[-a-z0-9.]+?.[-a-z0-9]{2,63})(?:^|\s|\n|[;,>"')])/gi, | |
emails = [], | |
dummy = emailStr.replace(re, function (match, email) { emails.push(email); return '[' + email + ']';}); | |
_.remove(emails, _.isEmpty); | |
return _.uniq(emails); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment