I am very close to getting this to work, but not quite there yet.
In my JavaScript code, I have a string with a list of email addresses, each formatted differently:
var emailList = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5525302130271525343b7b363a38">[email protected]</a>,
lucky <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b212a28200b3b243f65282426">[email protected]</a>>,
"William Tell" <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="197b7075756059... (content truncated for uniqueness)
My first task is to split this string into individual emails. Emails are separated by ', '
:
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cecf9e8f9eedcecfdf2b2fff3f1">[email protected]</a>, lucky <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7913181a123909160d571a1614">[email protected]</a>>
However, the separator ', '
might also appear within names enclosed in quotes:
"John Rambo, III" <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b6dcd9ded8d8cff6c4d7dbd4d998d5d9db">[email protected]</a>>
There can even be multiple commas inside quotes:
"there, might, be, several, commas inside the quotes" <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="65081009110c15090025060a08080416... (content truncated for uniqueness)
Step 1: replacing commas enclosed in quotes
I want to replace these commas with something like
<<<<!!!!>>>>
I attempted to achieve this using a regex pattern, but it's not working as expected:
(".*)(,)(\s.*"), $1<<<<!!!!>>>>$3
Step 2: splitting the array and reverting comma substitution
This can now be easily done with JavaScript using split and replace:
var Array = emailList.split(', ');
Array.forEach(function(element, index, arr) {
arr[index] = element.replace("<<<<!!!!>>>> ", ", ");
});
At this stage, I should have an array where each element represents an email in a specific format. Next step is to break down each individual email into its basic components.
Step 3: breaking down email addresses
To extract the relevant information from each email, including full name, first word, local part, and company, I'll need to use various regular expressions.
Overall Goal:
I'm looking for assistance to complete steps 1 and 3 more efficiently. Any optimized regex patterns or suggestions on how to improve the process would be greatly appreciated!
While it's not necessary, if you're able to craft a single magical RegExp that accomplishes the desired email breakdown, I will be thoroughly impressed and humbled by your Regex expertise! :)
Thank you for your help!