0

How can I take an e-mail address from "XXX <email@email.com>" ? I don't want to get the "< >".

Thanks!

donald
  • 23,587
  • 42
  • 142
  • 223
  • Could you provide a better example of the kind of input you're working with, and what you mean by "the < >"? – BHSPitMonkey May 06 '11 at 19:12
  • The input is within the string. Is when you get an email as: "First Last ". I want to know which email is it. – donald May 06 '11 at 19:13

7 Answers7

4

Here's one based on Tejs' answer. Simple to understand and I think a bit more elegant

// Split on < or >
var parts = "XXX <email@email.com>".split(/[<>]/);
var name = parts[0], email = parts[1];
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
3

Really simply (no need for regex!)

var components = emailAddress.split('<')

if(components.length > 1)
{
    var emailAddress = components[1].replace('>', '');
}
Tejs
  • 40,736
  • 10
  • 68
  • 86
3
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);

credit: How to find out emails and names out of a string in javascript

Community
  • 1
  • 1
neebz
  • 11,465
  • 7
  • 47
  • 64
1

This regex will work for your example.

/<([^>]+)/

It searches for anything after the '<' that is not a '>' and that is returned in your matches.

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
Leons
  • 2,679
  • 1
  • 21
  • 25
1
^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$

Matches e-mail addresses, including some of the newer top-level-domain extensions, such as info, museum, name, etc. Also allows for emails tied directly to IP addresses.

isxaker
  • 8,446
  • 12
  • 60
  • 87
  • 1
    This may even work, but it looks like Greek to me. Answers should always have some kind of explanation. – Ruan Mendes May 06 '11 at 19:20
  • This is doing much more than was asked, I think it confuses more than helps. This is for email validation, not to strip an email out of the string provided by the user. – Ruan Mendes May 06 '11 at 19:30
0

Not positive if I'm understanding you correctly. If you want to get the email domain ie gmail.com or hotmail.com then you could just use

var x =string.indexOf("@");  var y =string.subString(x)

this will give you the string y as the email domain.

Daniel Nill
  • 5,539
  • 10
  • 45
  • 63
0

To just grab what's inside the angle brackets, you can use the following:

var pattern = /<(.*)>/;
pattern.exec("XXX <foo@bar.com>"); // this returns ["<foo@bar.com>", "foo@bar.com"]
BHSPitMonkey
  • 654
  • 5
  • 14