Help make regex include commas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2023 06:09 AM
I have a regex that looks at an email's headers and it pulls out the people's emails in the CC list.
The problem is, it only pulls out the first email address and it's possible there are multiple emails in this list. How can I adjust this so it captures all of them?
The regex we are using is:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2023 06:43 AM
Hi @e_wilber,
Where do you want to use this?
If you are using it in an inbound action you could just user email.copied to get a comma seperated list of the emailaddresses in cc.
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2023 06:59 AM
Hi @e_wilber ,
please use the below regex script which I used in the background script to fetch the email address.
var email = 'to: fnam lname<email@test.com> cc:fnam lname1<email1@test.com>,fnam lname2<email2@test.com>,fnam lname3<email3@test.com>,fnam lname4<email4@test.com>';
var ccLine = email.match(/cc:(.*)/)[1];
var emailAddresses = ccLine.match(/\b\w+@\w+\.\w+\b/g);
gs.print(emailAddresses.join(', '));
Result:
*** Script: email1@test.com, email2@test.com, email3@test.com, email4@test.com
I hope this helps!
Regards,
Hemant
**Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.**

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2023 07:10 AM
Nice solution if no Inbound action is used @Hemant Goldar,
Although this will fail when bcc addresses are also present in the line.
This filters these out as well:
var email = 'To: fnam lname<email@test.com> Cc:fnam lname1<email1@test.com>,fnam lname2<email2@test.com>,fnam lname3<email3@test.com>,fnam lname4<email4@test.com> Bcc: fnam lname<email@test.com> ';
var noToLine = email.split(/Cc:(.*)/)[1];
var noBccLine = noToLine.split(/Bcc:(.*)/)[0];
var emailAddresses = noBccLine.match(/\b\w+@\w+\.\w+\b/g);
gs.print(emailAddresses.join(', '));
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.