Help make regex include commas

e_wilber
Tera Guru

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: 

origCCList = email.headers.match(/Cc:\s*.*<([^>]*)>/)[1];
 
The email headers looks like:
e_wilber_0-1692018534121.png

 

3 REPLIES 3

Peter Bodelier
Giga Sage

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.

Hemant Goldar
Mega Sage
Mega Sage

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.**

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.