JavaScript Regular Expression for separating a collection of email addresses into individual parts

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!

Answer №1

I propose that achieving your desired outcome is possible with the following regex pattern:

(?:(?:"?((\w+)\b.*\b)"?)\s)?<?(([\w@]*)@(\w*)\.[a-zA-Z]{2,3})>?,?

and utilizing the replacement code:

{ fullName:'\1', firstWord:'\2', localPart:'\4', company:'\5', email:'\3'}

Check out the Demo

Answer №2

To split a string at commas excluding those within quotes, you can utilize the regex pattern shown here:

,(?=(?:[^'"]|'[^']*'|"[^"]*")*$)

With this method, you can skip steps 1 and 2.

As for the ineffective patterns in step 3:

This pattern fails as it captures trailing "

  • (?|"(\[^"\]+)"|(.*) <): initially matches balanced quotes or anything before <.
    Note: Group 2 must be checked if group 1 is empty (unfortunately, JS does not support branch reset groups).

This pattern fails to capture "peter" in "peter@pan"

  • (<|^)(.*)@: could potentially match starting from the beginning;
    however, anchoring the pattern correctly poses challenges.

For email validation, consider using one of the established and recommended solutions provided here and on this website. However, that's a separate discussion altogether.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to confirm checkbox selection based on MySQL data?

Writing this question feels challenging, but I have a collection of checkboxes with their data stored as JSON in my PHP database. What I'm trying to achieve now is to dynamically load the JSON data on the page without refreshing it, checking specific ...

Adjusting package.json settings for an npm module

An npm package that I am using has an incorrect file path specified in its package.json. The current configuration is: "main": "./htmldiff.js", However, for it to function correctly, it should be: "main": "./src/html ...

"My JavaScript code for toggling visibility is not functioning properly. Any suggestions on how to fix

I am currently working on a task that involves showing and hiding container1, but I am confused as to why my code is not functioning properly. The task requirements are as follows: - Clicking on the "Show" button should display container1 - Clicking on the ...

Tabbed form design using Twitter Bootstrap

Currently, I am working on a website using the Bootstrap framework. My goal is to create a single page that features a pop-up form with two or three tabs. Specifically, I envision these tabs as "The Markup," "The CSS," and "The JavaScript." While I have ma ...

Why does the node.js app only run from the command prompt and not directly?

I have a basic vbscript that launches two nodejs apps necessary for my server's operations. Dim objShell Set objShell = Wscript.CreateObject("WScript.Shell") objShell.Run "node C:\!webroot\site.name\server\pubsub.js" objShell.Run ...

Trouble with a basic Angular demonstration

After replicating an angular example from w3schools (found here), I encountered some issues with it not functioning correctly. Despite my efforts, the code appears to be accurate. Can anyone spot what might be going wrong? To provide more context, here is ...

Generating distinctive content within the confines of the Selenium WebDriver

Is there a way to generate a unique username value for the signup page username textbox using selenium webdriver instead of hardcoding it? For example: driver.findElement(By.id("username")).sendKeys("Pinklin") ; When "Pinklin" is hardcoded, running the ...

Changing the disabled textfield colour in Material-UI

I am looking to enhance the visibility of disabled text in a textfield by making it darker than the current light color: I have attempted to achieve this using the following code snippet: import { withStyles } from '@material-ui/styles'; import ...

transfer to a different outlet when needing to circle back earlier

I'm currently working on implementing validation in a form, and one of the needed validations involves retrieving a value from an input field and checking its existence on the server or in a JSON file. However, there seems to be a problem where even ...

Sending the method's URL in the controller through an AJAX call

Below is the code snippet for an ajax call: <script> jQuery(document).ready(function() { $("#VEGAS").submit(function(){ var form_data = $("#VEGAS").serialize(); var routeUrl = "<?= url('/'); ?> /PUBLIC/vpage"; $.ajax({ ...

Having trouble understanding how to display an HTML file using Next.JS?

Currently, I am working on a project that involves utilizing Next.JS to develop a webpage. The main goal is to display a PDF file on the left side of the screen while integrating Chaindesk on the right side to create a chat bot capable of extracting inform ...

Collaboratively utilizing resources among various NPM Workspaces

Currently, I am working on a React project using NPM Workspaces. I have created an 'assets' workspace within this project to store all images and assets that need to be accessed by other workspaces. The directory structure of the project is as fo ...

Can someone explain why my search input's sync function is being triggered twice?

Within my CodePen project at https://codepen.io/aaronk488/pen/MWbKNOq?editors=1011, I am facing an issue where my sync function search is being called twice without a clear reason. Even though I managed to resolve this by adding a conditional statement in ...

What is the step-by-step process for incorporating the `module` module into a Vue project?

ERROR Compilation failed with 6 errors 16:20:36 This specific dependency could not be located: * module in ./node_modules/@eslint/ ...

Can regular expressions be employed to match URLs with $urlRouterProvider in AngularJS?

My goal is to create a regex expression that matches all strings in my routing config which do not start with "/pt/" or "/en/". Additional text may be present in these strings. This is what I have so far: $urlRouterProvider.when('^(?!/(pt|en)/).*&ap ...

The AngularJS directive within a directive is failing to properly initialize the scope value

In my current setup, I am working with a controller that contains the value $scope.colorHex. As an example, I am utilizing the directive colorpickerTooltip, and within its template, I am calling another directive: <colorpicker ng-model="colorHex">&l ...

JavaScript Document Object Model: Locate an element with a class that is either similar to or includes a specific substring

I am facing a situation where I need to locate a wrapper element with a specific class name that includes a random id, such as wp-rgtayfu-fxyjzw-wrapper. The class will always have the substring wrapper in it, and there is only one element in the document ...

I need to know the right way to send a file encoded in Windows-1255 using Express

I'm currently developing an API that is responsible for generating text files. The purpose of this API is to support legacy software that specifically requires the use of Windows 1255 encoding for these files. To create the content of the file, I am r ...

Steps for implementing a Toggle Navigation Bar in CSS

I'm looking to implement a show/hide navigation menu similar to the one showcased in this inspiration source: Code Snippet (HTML) <div id="menus"> <nav id="nav"> <ul> <li><a href="#">HOME</a></li> <li& ...

JavaScript problem with hovering action causing additional buttons to appear

Currently, I am developing a user interface where, upon hovering over an LI element, 2 buttons appear to provide additional functionality - "edit" and "remove". However, I am facing challenges with the mouse hit zones. The mouseover function works effect ...