1

So, I have this JavaScript function:

ME.Utils = {
    RxEmail: new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i),

    ValidateEmail: function(email) {
        return ME.Utils.RxEmail.test(email);
    },

    GetEmailAddresses: function(text) {
        return text.match(ME.Utils.RxEmail);
    },

    HasEmail: function(text) {
        return ME.Utils.GetEmailAddresses != null;
    }
};

ValidateEmail works very well. However, HasEmail and GetEmailAddresses is not working properly.

GetEmailAdresses always returns null, except for when the string only contains an email address. In this case, GetEmailAdresses returns an array not only containing the email address, but the email address (test@test.com), just the id (test) plus some unidentified etc. etc...

Can you help me figure out what's wrong in my expression?

Mickel
  • 6,658
  • 5
  • 42
  • 59
  • 1
    Oh look. Someone is trying to test email addresses with a regular expression. When was the last time I saw one that succeeded? Not today, it rejects email addresses with + characters in them. – Quentin May 06 '10 at 08:13
  • 7
    Please stop mocking me. help me, or say nothing at all... – Mickel May 06 '10 at 08:16
  • @david, it's a simple matter to add a + to the regexp. what is wrong with a decent regexp test for email? – chris May 06 '10 at 08:22
  • 1
    @odeo, its not that simple. see http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html and http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses and even that monster requires that some stripping is done first – Sam Holder May 06 '10 at 08:43
  • There was a similar question on regexing email ids a couple of months ago, wasn't there? – Blessed Geek May 06 '10 at 08:46
  • 1
    Questions about parsing email addresses with regex cropup here on a regular basis, and they usually have false negatives on common address styles. I'm not mocking you, I'm expressing frustration at people failing to learn from history and making the same mistakes over and over again. Regular expressions are not suited to parsing email addresses in any depth but people insist on trying and failing again and again. Yahoo! is a huge company who's business is built about the web, and they can't get it right: http://twitter.com/edent/status/11687784947 - but so many smaller fish think they can. – Quentin May 06 '10 at 08:51
  • 1
    @h2g2java: Try every few days: http://stackoverflow.com/search?q=email%20[regex]&tab=newest – Quentin May 06 '10 at 08:52

4 Answers4

4

There are a few problems.

  1. Your regex is anchored at the start and end of the string. You should remove the ^ and $ characters from it.

  2. If you want to return only the email addresses, use non-capturing groups.

  3. In HasEmail(), you're not calling GetEmailAddresses(). You're actually checking if the value of that property is defined.

All in all, a fixed version might look like:

ME.Utils = {
    RxEmail: /(?:(?:"[\w-\s]+")|(?:[\w-]+(?:\.[\w-]+)*)|(?:"[\w-\s]+")(?:[\w-]+(?:\.[\w-]+)*))(?:@(?:(?:[\w-]+\.)*\w[\w-]{0,66})\.(?:[a-z]{2,6}(?:?:\.[a-z]{2})?))|(?:@\[?(?:(?:25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?)/gi,

    ValidateEmail: function(email) {
        // We can't do a simple test() since we're using an unanchored regex now.
        var match = ME.Utils.RxEmail.match(email);
        return match.length == 1 && match[0] == email;
    },

    GetEmailAddresses: function(text) {
        return text.match(ME.Utils.RxEmail);
    },

    HasEmail: function(text) {
        return ME.Utils.GetEmailAddresses(text) != null;
    }
};
Max Shawabkeh
  • 37,799
  • 10
  • 82
  • 91
2

your regexp specifically matches an entire string

RxEmail: new RegExp(/^ ... $)/i),

the ^ and $ match the start and end of the input respectively. try removing those characters and see how you get on?

chris
  • 9,745
  • 1
  • 27
  • 27
1
if (emailMatch = ME.Utils.GetEmailAddresses(myEmail))
  // do stuff with emailMatch[1]
Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
  • not very helpful. I need to get an array with all email addresses in the string, like ["test@test.com", "sth@sth.com", etc@etc.com"]. Right now GetEmailAddresses returns an array full of junk. – Mickel May 06 '10 at 08:12
1

The reason you're getting multiple entries in the array when presenting it with an email address is that you have capture groups in your expression. In Javascript, the result of match is an array where index 0 is the total string matched, and then there are (optionally) additional indexes for each capture group. You can make your groups non-capturing by changing them from (...) to (?:...).

As for why you're not getting the results you expect when using GetEmailAddresses with a string containing an email address, try this instead:

GetEmailAddresses: function(text) {
    var rv = [];
    var match;

    while (match = ME.Utils.RxEmail.exec(text)) {
        rv.push(match[0]);
    }
    return rv.length == 0 ? null : rv;
},

See this question and answer, I can't say I know why String#match isn't quite the same as the RegExp#exec loop above, but it isn't.

Edit And you'll need to fix the issue that oedo pointed out as well; the RegExp needs to be allowed to match substrings.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875