2

I have a form field that contains one , or several "Complete" email addresses. By "Complete" I mean in the format Personal Name <person@domain.com>

Multiple "complete" email address are separated by a semi-colon. There may or may not be a final semi-colon at the end of the list. I want to extract the Names into one field and the email addresses into another field.

so :

full_email = "Person name <parson@domain.com> ; Another person <another@domain.com> ; " 
email_only is set to  "parson@domain.com ; another@domain.com ;" 
name_only is set to  "Person name ; Another person ;"

Is there some reg exp / jQuery bit of genius to copy everything between the < and > into a second field and everything before the < into a third field and do it iteratively/recursively for the whole list of "complete" email addresses to produce two semi-colon separated lists of just pure email address and just pure names?

Paolo Bergantino
  • 449,396
  • 76
  • 509
  • 431
  • Please look at the preview when posting. Without code formatting (four spaces at the start of the line) any text inside `<>` is lost. – Anders Abel Apr 24 '12 at 19:39

4 Answers4

2

I think you can do this without regex or jQuery by simply splitting on ; to get the entries, then on < to get the parts of the entry. Here's a jsFiddle to demonstrate.

var full_email = "Person name <parson@domain.com> ; Another person <another@domain.com> ; " ;

var email_only = [];
var name_only = [];

var entries = full_email.split(';');

for(var i = 0; i < entries.length; i++)
{
    var entry = entries[i];
    if (entry.contains('<')){
        var parts = entry.split('<');

        name_only.push(parts[0].trim());
        email_only.push(parts[1].substr(0, parts[1].indexOf('>')));
    }
}

// The arrays
console.log(email_only);
console.log(name_only);

// Joined back to ; separated strings
console.log(email_only.join('; '));
console.log(name_only.join('; '));
David Ruttka
  • 13,641
  • 2
  • 40
  • 39
  • Thanks for this. I guess once I got onto a jQuery frame of mind I couldn't see a solution beyong that. I've modified this slightly and it works fine. – Matt Walker-Wilson May 01 '12 at 12:31
1

You can use the split method in javascript to split first each address ...

var emails = "";
var names = "";
$.each(full_email.split(';'), function(index,val){
    var contact_array = val.split('<');
    var name = contact_array[0];
    var email = contact_array[1];
    email = email.substring(0,email.indexOf('>'));
    emails += email + "; ";
    names += name + "; ";
});

So you can to whatever you want with the names and emails. You can also use the trim method on the names, hope this is what you're looking for.

Community
  • 1
  • 1
sergioadh
  • 1,451
  • 1
  • 16
  • 24
  • 2
    Don't use `.each()` for collections that are not DOM elements. Use `$.each(collectiom, function(idx, val) {})` instead. – ThiefMaster Apr 24 '12 at 20:03
1

Here's an all JavaScript way to build two arrays out of your input:

var full_email = "Person name <parson@domain.com> ; Another person <another@domain.com> ; " 
var email_only = new Array();
var name_only = new Array();

step1= full_email.split(';');
for(var i=0;i<step1.length-1;i++){
    step2 = step1[i].trim();
    step3 = step2.split('<');
    email_only.push(step3[1].slice(0,-1).trim());
    name_only.push(step3[0].trim());
}

console.log(email_only,name_only);

​jsFiddle example.

j08691
  • 190,436
  • 28
  • 232
  • 252
0

If you remove the unnecessary spaces from you full_name string this script will handle it. Fiddle: http://jsfiddle.net/GvGoldmedal/CacU8/

var full_email = "Person name <parson@domain.com>;Another person <another@domain.com>;";
var array = full_email.split(";");

var substr = "";
var subsplit = "";
var names = new Array();
var emails = new Array();


for( var x=0; x < array.length - 1; x++)
{
    substr = array[x];
    subsplit = substr.split(" ");
    //Email and Name Exists
    if (subsplit.length > 2)  
    {
        names.push(subsplit[0] + " " + subsplit[1]);
        subsplit[2] = subsplit[2].replace("<", "");
        subsplit[2] = subsplit[2].replace(">", "");
        emails.push(subsplit[2]);
    }
    else if (subsplit.length = 2) // Only Name
    {
        names.push(subsplit[0] + " " + subsplit[1]);
    }
    else // only email
    {
        subsplit = subsplit.replace("<", "");
        subsplit = subsplit.replace(">", "");
        emails.push(subsplit);
    }
}

for (var x=0; x < names.length; x++)
{
    alert("Name:" + x + " " +names[x]);

}

for (var x=0; x < emails.length; x++)
{
    alert("Email:" + x + " " +emails[x]);

}
Matt Moore
  • 581
  • 2
  • 6