Skip to content

Instantly share code, notes, and snippets.

@ttsukagoshi
Created April 14, 2021 14:23
Show Gist options
  • Save ttsukagoshi/3cf305cef182569c681090fd50ebdf8b to your computer and use it in GitHub Desktop.
Save ttsukagoshi/3cf305cef182569c681090fd50ebdf8b to your computer and use it in GitHub Desktop.
Parse the string of recipients returned by Gmail into raw email addresses. For example, if Gmail provides a recipient string in form of `George Washington <gwashington@potus.com>`, this function will return `gwashington@potus.com`
/**
* Parse the string of recipients returned by Gmail into raw email addresses.
* For example, if Gmail provides a recipient string in form of
* "George Washington <gwashington@potus.com>"
* this function will return
* "gwashington@potus.com"
* @param {string} gmailRecipients String of comma-separated recipients returned by GmailMessages.getTo(), .getCc(), or .getBcc()
* @returns The raw email addresses of gmailRecipients, without recipient name and angle brackets
*/
function getRawEmail_(gmailRecipients) {
var regexp = /<([^\>]+)>/;
return gmailRecipients.split(',').map(recipient => {
let matchResult = recipient.match(regexp);
if (!matchResult) {
return recipient;
} else {
return matchResult[1];
}
}).join(',');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment