Skip to content

Instantly share code, notes, and snippets.

@mscalora
Created March 21, 2018 16:53
Show Gist options
  • Save mscalora/06173ef3e7595b003e6e5601343f098c to your computer and use it in GitHub Desktop.
Save mscalora/06173ef3e7595b003e6e5601343f098c to your computer and use it in GitHub Desktop.
Parse emails from text
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