title: RegExp for splitting a list of emails into basic components (JavaScript)
body: <p>I almost have this working, but not quite.</p>
  
  <p>I have a JavaScript string that contains a list of emails each formatted differently (no newlines, edited for legibility's sake):</p>
  
  <pre><code>var emailList = 'peter@pan.com, 
  lucky &lt;jack@pot.com&gt;, 
  "William Tell" &lt;billy@tell.com&gt;, 
  "John Rambo, III" &lt;johnny@rambo.com&gt;, 
  "there, might, be, several, commas inside the quotes" &lt;multiple@commas.com&gt;, 
  "yes, this is also a valid email address, can you believe" &lt;yes@this@is@valid.com&gt;'
  </code></pre>
  
  <p>Firstly, I need to split this string into the different emails. Emails are separated by <code>', '</code>:</p>
  
  <p><code>peter@pan.com, lucky &lt;jack@pot.com&gt;</code></p>
  
  <p>but <code>', '</code> also might occur in names enclosed by quotes:</p>
  
  <p><code>"John Rambo, III" &lt;johnny@rambo.com&gt;</code></p>
  
  <p>Even multiple commas can be found inside quotes:</p>
  
  <p><code>"there, might, be, several, commas inside the quotes" &lt;multiple@commas.com&gt;</code></p>
  
  <blockquote>
    <p>Step 1: replace <code>,</code> enclosed in quotes</p>
  </blockquote>
  
  <p>I'd like to substitute the commas for something like <code>&lt;&lt;&lt;&lt;!!!!&gt;&gt;&gt;&gt;</code></p>
  
  <p>I've tried this regexp: <code>(".*)(,)(\s.*"), $1&lt;&lt;&lt;&lt;!!!!&gt;&gt;&gt;&gt;$3</code> <a href="https://regex101.com/r/baha69/1/" rel="nofollow noreferrer">https://regex101.com/r/baha69/1/</a> but it's NOT replacing commas within quotes... :-(</p>
  
  <blockquote>
    <p>Step 2: split array and undo comma substitution</p>
  </blockquote>
  
  <p>This can be easily done now in JavaScript with split and replace:</p>
  
  <pre><code>var Array = emailList.split(', ');
  Array.forEach(function(element, index, arr) {
    arr[index] = element.replace("&lt;&lt;&lt;&lt;!!!!&gt;&gt;&gt;&gt; ", ", ");
  });
  </code></pre>
  
  <p>at this point, I should have an array like this (no newlines, edited for legibility's sake):</p>
  
  <pre><code>Array[0] = 'peter@pan.com'
  Array[1] = 'lucky
              &lt;jack@pot.com&gt;'
  Array[2] = '"William Tell"
              &lt;billy@tell.com&gt;'
  Array[3] = '"John Rambo, III"
              &lt;johnny@rambo.com&gt;'
  Array[4] = '"there, might, be, several, commas inside the quotes
              &lt;multiple@commas.com&gt;'
  Array[5] = '"yes, this is also a valid email address, can you believe"
              &lt;yes@this@is@valid.com&gt;'
  </code></pre>
  
  <blockquote>
    <p>Step 3: split email addresses</p>
  </blockquote>
  
  <p>Now I have to turn each individual email into basic components (no newlines, edited for legibility's sake):</p>
  
  <pre><code>Array[0] = {fullName: '',
              firstWord: '', localPart: 'peter', company: 'pan', 
              email: 'peter@pan.com'}
  Array[1] = {fullName: 'lucky',
              firstWord: 'lucky', localPart: 'jack', company: 'pot', 
              email: 'jack@pot.com'};
  Array[2] = {fullName: 'William Tell',
              firstWord: 'William', localPart: 'billy', company: 'tell',
              email: 'billy@tell.com'};
  Array[3] = {fullName: 'John Rambo, III',
              firstWord: 'John', localPart: 'johnny', company: 'rambo',
              email: 'johnny@rambo.com'};
  Array[4] = {fullName: 'there, might, be, several, commas inside the quotes', 
              firstWord: 'there', localPart: 'multiple', company: 'commas',
              email: 'multiple@commas.com'};
  Array[5] = {fullName: 'yes, this is also a valid email address, can you believe', 
              firstWord: 'yes', localPart: 'yes@this@is', company: 'valid',
              email: 'yes@this@is@valid.com'};
  </code></pre>
  
  <p>To do that I'll use the following RegExps:</p>
  
  <pre><code>var firstWord = element.match('/"?(\w*),? .*"?/ig')[1]; 
  </code></pre>
  
  <p>this works!! :-)
  <a href="https://regex101.com/r/6Z481l/1" rel="nofollow noreferrer">https://regex101.com/r/6Z481l/1</a></p>
  
  <pre><code>var fullName = element.match('/"?(.*)"? &lt;/ig')[1]; 
  </code></pre>
  
  <p>this DOESN'T work: captures trailing " :-(
  <a href="https://regex101.com/r/6Z481l/2" rel="nofollow noreferrer">https://regex101.com/r/6Z481l/2</a></p>
  
  <pre><code>var localpart = element.match('/&lt;(.*)@/ig')[1];
  </code></pre>
  
  <p>this DOESN'T work: peter in peter@pan is not captured :-(
  <a href="https://regex101.com/r/6Z481l/3" rel="nofollow noreferrer">https://regex101.com/r/6Z481l/3</a></p>
  
  <pre><code>var company = element.match('/@(.*)\./ig')[1];
  </code></pre>
  
  <p>this works!! :-)
  <a href="https://regex101.com/r/6Z481l/4" rel="nofollow noreferrer">https://regex101.com/r/6Z481l/4</a></p>
  
  <pre><code>var email = element.match('/&lt;(.*@.*)&gt;|(^[^&lt;].*[^&gt;])/ig')[1];
  </code></pre>
  
  <p>surprisingly, this works!! :-) But I'm almost certain it can be made more elegant
  <a href="https://regex101.com/r/6Z481l/5" rel="nofollow noreferrer">https://regex101.com/r/6Z481l/5</a></p>
  
  <blockquote>
    <p>Worth mentioning, emails are presumed to be validated</p>
  </blockquote>
  
  <p>So, I need some help to complete steps 1 and 3. If any working regexp from step 3 can be simplified or made more elegant, I'll learn from that!</p>
  
  <p>Not the goal, but if you come up with ONE magic RegExp that splits the email like I need it, then I can guarantee you are going to certainly wow me and make me feel very small for my lack of RegExp knowledge!!! :-)</p>
  
  <p>Thanks!</p>