4

I am using a cool widget to import email addresses out of gmail/homail/yahoo etc. The widget is still beta and I guess thats why it does not allow a lot of configuration. It actually just fills a textarea with the following data:

"Name one" <foo@domain.com>, "Name Two" <foo@domain.com>, "And so on" <andsoon@gmx.net>

So I wondered if someone could help me write a regex or something like that to get all values out ofa string into an array. The desired format would be:

[{name: 'Name one', email: 'foo@domain'},{name: 'Name Two', email: 'foo@domain'},{name: 'And so on', email: 'andsoon@gmx.net'}]

I am a total regex noob and I have no clue on how to do that in javascript. Thanks for your help!

Marc
  • 377
  • 7
  • 19
  • Be warned that parsing email addresses is, in general, *extremely* complicated. If you try to cheat, you'll find some legitimate user with an email address that breaks your regex in about 15 minutes after you go live :-) – Pointy Mar 02 '10 at 16:34
  • I am sorry. I missformulated the question a little bit: It is not important to check wheter an email address is correct or not. The widget will only return valid emails. What I don't understand is to get the pairs of data (name + email). I would like to find out what is inside "" => name and what is inside <> = email. string.match does only find one at a time. right? – Marc Mar 02 '10 at 16:53
  • Even if you're not checking the validity of the email addresses it's going to be tricky. There can be commas inside the quoted "name" parts of each address, for one thing. If it were me, I'd be spending my energy trying to figure out how to get my "cool widget" to give me the email addresses in a programming-friendly way in the first place. – Pointy Mar 02 '10 at 16:55

3 Answers3

11
function getEmailsFromString(input) {
  var ret = [];
  var email = /\"([^\"]+)\"\s+\<([^\>]+)\>/g

  var match;
  while (match = email.exec(input))
    ret.push({'name':match[1], 'email':match[2]})

  return ret;
}

var str = '"Name one" <foo@domain.com>, ..., "And so on" <andsoon@gmx.net>'
var emails = getEmailsFromString(str)

Tracker1
  • 19,103
  • 12
  • 80
  • 106
  • The global flag can cause problems if you run this method many times. You will then experience that it works the first time, fails the next one, works the third time. – dparnas Apr 02 '14 at 12:53
  • @dparnas, that is why I am defining the regexp inside the function... the next time the function is run, you will get a new regex instance. If it was defined outside of the function, the behavior your mention would happen. – Tracker1 Apr 02 '14 at 18:31
  • @tracker1 actually encountered the problem even when defining the regexp inside the function. But this was a Google Apps Script, so that may be the reason – dparnas Apr 03 '14 at 07:57
3
function findEmailAddresses(StrObj) {
        var separateEmailsBy = ", ";
        var email = "<none>"; // if no match, use this
        var emailsArray = StrObj.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
        if (emailsArray) {
            email = "";
            for (var i = 0; i < emailsArray.length; i++) {
                if (i != 0) email += separateEmailsBy;
                email += emailsArray[i];
            }
        }
        return email;
    }

Source here

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
karim79
  • 339,989
  • 67
  • 413
  • 406
  • 1
    And if my email address is Håkan.Söderström@malmö.se? ;-) (See: http://stackoverflow.com/questions/3232/how-far-should-one-take-e-mail-address-validation/300862#300862 ) Also, don't forget that `+` is a valid character for the name. Gmail users like to filter their emails `with me+label@gmail.com` – Sean Vieira Mar 02 '10 at 16:43
  • That said, I don't think any regex is perfectly suited to this particular purpose, but do I think this answer does demonstrate the theory of operation of extracting emails from an arbitrary string. – karim79 Mar 02 '10 at 16:49
  • I am sorry. I missformulated the question a little bit: It is not important to check wheter an email address is correct or not. The widget will only return valid emails. What I don't understand is to get the pairs of data (name + email). I would like to find out what is inside "" => name and what is inside <> = email. string.match does only find one at a time. right? – Marc Mar 02 '10 at 16:53
0

In order to acheive that you'll have to read some documentation. Regex are not very complicated, but they get some gettin used to.

Here's a nice place to start Javascript Regular Expression

And try practicing writting regex using rubular

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
marcgg
  • 65,020
  • 52
  • 178
  • 231