Get all email addresses in a string with JavaScript

Posted by Mickel on Stack Overflow See other posts from Stack Overflow or by Mickel
Published on 2010-05-06T08:06:05Z Indexed on 2010/05/06 8:08 UTC
Read the original article Hit count: 271

Filed under:
|
|

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 ([email protected]), just the id (test) plus some unidentified etc. etc...

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

© Stack Overflow or respective owner

Related posts about regex

Related posts about JavaScript