6

What I am trying to do is to extract email address out of the string SomeName, First ([email protected])

Here is the code that I already tried:

 var stringToSearchIn="SomeName, First ([email protected])";


 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

var extractedEmail=re.exec(stringToSearchIn);

The variable extractedEmail in the code returns a null.

5
  • possible duplicate stackoverflow.com/questions/11312662/regex-for-email-matching Commented Feb 28, 2013 at 16:45
  • 2
    If the string is always in this format, why not just use ( and ) as delimiters? No need for a regex then. Commented Feb 28, 2013 at 16:52
  • @Jay That's a useless edit now, as someone can always retrieve this question's history. Commented Feb 28, 2013 at 17:23
  • I had changed the email address before posting it. That email address was non existent. Commented Feb 28, 2013 at 18:20
  • Next time, when you want to use a fictive email adress, let it end with @example.com. Commented Feb 28, 2013 at 19:28

4 Answers 4

7

I tried this and failed

...is not a very useful description of what happenned. At a guess, the re failed to find a match. An obvious cause for this is the regex you've used here will only match a string which ONLY contains an email address - remove the end anchors (^,$) from the regex and you'll get your email address.

var re = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
Sign up to request clarification or add additional context in comments.

2 Comments

@Foo: You can just use extractedEmail[0].
Sorry guys, cannot up vote answer due to lack of reputation points :(
1

No need for a regex here. If the string is always of the format

Name (email)

You can simply use ( as delimiter:

var stringToSearchIn="SomeName, First ([email protected])";

var extractedEmail = stringToSearchIn.substr(stringToSearchIn.indexOf('(') + 1).slice(0, -1);

Comments

0

This one works for me.

var searchInThisString ="SomeName, First ([email protected]) SomeName2, First2 ([email protected])";

var foundEmails =[];

var emailRegex = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;

var gotcha;

while (match = emailRegex.exec(searchInThisString)){

     //-- store in array the found match email
     foundEmails.push(match[0]);

    //-- remove the found email and continue search if there are still emails
    searchInThisString= searchInThisString.replace(match[0],"")
}

alert(foundEmails.join(" "))

1 Comment

Who is gotcha and why is match global?
-1

Try this regex

@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"

This will accept any of the following:

and so on...

2 Comments

I haven't tested to see if this works, and I'm not going to. It could be a very useful regex, but you haven't explained at all why this could be a very useful regex.
It won't solve the OP's problem and there are multiple issues with the validation of the address using this - it will only match something which looks vaguely like an email address

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.