2

I'm trying to match the email regex \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b with a string in Javascript. At the moment I'm using the code email.match(/b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/) but it doesn't match any email addresses. Do regex's need to be changed before they are used in Javascript?

Tom
  • 21
  • 1
  • 4
    TLDs are not limited to having 2-4 letters in them… – Quentin Jul 04 '11 at 10:00
  • 3
    For that matter, [they aren't limited to latin script](http://news.cnet.com/8301-1023_3-20004429-93.html) either – Quentin Jul 04 '11 at 10:02
  • 1
    Can you show us your actual code that's not working, not just the regex? (BTW, using a regex to validate an email address is generally considered a bad idea, since it's actually [impossible](http://stackoverflow.com/questions/156430/regexp-recognition-of-email-address-hard)) – Jonathan Hall Jul 04 '11 at 10:03
  • 1
    @Flimzy - agreed. I just check for the existence of the "@" as a basic sanity check: http://stackoverflow.com/questions/6533344/check-for-valid-email-before-running-remaining-javascript/6533395#6533395 – Richard H Jul 04 '11 at 10:04
  • 1
    I'm not sure about the first 'b'? Do you really want just email addresses starting with a 'b'? – flori Jul 04 '11 at 10:04
  • @flori - \b is a word boundary, presumably he's typo'd in the match() expression – Richard H Jul 04 '11 at 10:06
  • @Richard H: Okay, a bit confusing in the second example the \ is missing. – flori Jul 04 '11 at 10:09

2 Answers2

5

Problems matching email addresses with regex aside:

You have to add the case-insensitive modifier since you are only matching uppercase characters. You also are missing the \ in front of the b (which makes the expression match a b literally) and the \b at the end (thanks @Tomalak) (even though it will "work" without it):

email.match(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i)

If you only want to know whether the expressions matches or not, you can use .test:

patter.test(email)

More about regular expressions in JavaScript.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    And the `\b` at the end. I think the OP made a misunderstanding when adding delimiters. – Lightness Races in Orbit Jul 04 '11 at 10:05
  • [This](http://www.regular-expressions.info/email.html) appears to be the source of the regex, and yes, it is supposed to be case-insensitive and have a `\b` at either end. Excellent article. – Alan Moore Jul 04 '11 at 12:41
0

You should use a RFC 2822 compliant RegEx for validating emails, even if it's a big one;

function check_mail(str){
    var reg=new RegExp(/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/i)

  if(str.match(reg)){
    return true;
  }else{
    return false;
  }
}

For more details on validating email using RegExs see regular-expression.info

monsieur_h
  • 1,360
  • 1
  • 10
  • 20
  • 1
    No No I humbly disagree. There are real addresses that _don't_ comply with the RFC. Plus you've no idea if a valid format is a real email anyway, so what's the point. Far easier and more maintainable just to check for the "@" character as a basic sanity check. – Richard H Jul 04 '11 at 10:23
  • Indeed, it all depends on what you are looking for: Testing a real email implies sending a mail to the adress. Testing a format can be limited to the "@" if you need a really quick test. I use my solution as an average one. – monsieur_h Jul 04 '11 at 10:26