How do I split a list of emails with multiple formats in JavaScript?



I have a list of emails and the email have two formats:

  1. with name name <>
  2. without name

I am wondering if there is a way to split this list with different separators in Javascript.

List of examples :,name1 <>,
or name2 <>
name1 <>


For the moment I split the list with the three separators for the simple email format and I still confused with how to do it including the name format name <>?

Here is the code I used for that:

    var emailInput = $(this);
    var clipboardData = e.originalEvent.clipboardData || window.clipboardData;
    var pastedData = clipboardData.getData('Text');
    var emails = pastedData.split(/[\r,\s]+/);

What I need to extract for example name <>
==> ["","name <>"]


Assuming you are not looking for an email address validator, and already know that your list has email addresses, then you can do:

let data = `,name2 <>,,, name9 <>
name13 <>

let result = data.match(/[^,;<>\s]+@[^,;<>\s]+|[^,;<>\s]+\s+<[^,;<>\s]+@[^,;<>\s]+>/g);


Answered By – trincot

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More