0
validate_email = ( email ) ->
   (/^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/).test email

This works, but it fails my "max line length" test at 79 characters, so I've been trying to cut it down to less than 80 characters (actually max 74) using the example at coffeescript.org:

RFC822 = /// ^ (
  ([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+
  | \x22([^\x0d\x22\x5c\x80-\xff]
  | \x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a
  -\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+
  | \x22([^\x0d\x22\x5c\x80-\xff]
  | \x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a
  -\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+
  | \x5b([^\x0d\x5b-\x5d\x80-\xff]
  | \x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a
  -\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+
  | \x5b([^\x0d\x5b-\x5d\x80-\xff]
  | \x5c[\x00-\x7f])*\x5d))*$/
) ///
RFC822.test email

Although this compiles just fine, it isn't working. It always returns false.

How do I do this?

  • What's with the 80 characters restriction? – Felix Kling Apr 12 '13 at 10:34
  • What's with it? It's just in the tests I run and something I ask others working on the project to abide by (so breaking it would set a bad example). Currently this is the only line that fails the test. –  Apr 12 '13 at 10:37

2 Answers2

3

You can build from string (if you escape the backslashes):

var re = new RegExp(
               "..." + 
               "..." + 
               "...");

(gist of full matcher in JS, seems to work okay, my CF is rusty though so I don't know if it translates to CF)

robertklep
  • 198,204
  • 35
  • 394
  • 381
  • Why didn't I think of that! Works perfectly of course. Thanks! –  Apr 12 '13 at 11:04
  • @GormCasper actually you only had a couple of character errors in your original code, which I was able to easily fix and recreate the original one liner. – Alnitak Apr 12 '13 at 11:08
  • And I corrected the accepted answer to yours. Both these answers work though! :) –  Apr 12 '13 at 11:26
2

How do I do this?

You don't - regular expressions are not suitable for parsing email addresses.

Any such regexp is doomed to failure, because domain names now exist which break almost every assumption typically made by them, e.g.:

  • long ASCII TLDs (I've seen regexps which barf if the TLD is longer than 4 chars)
  • international domains (IDNs) which don't use US ASCII

That said, using the interactive Coffeescript interpreter I was able to just tweak a few characters and get this:

RFC822 = ///
^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+
  | \x22([^\x0d\x22\x5c\x80-\xff]
  | \x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a
  -\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+
  | \x22([^\x0d\x22\x5c\x80-\xff]
  | \x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a
  -\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+
  | \x5b([^\x0d\x5b-\x5d\x80-\xff]
  | \x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a
  -\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+
  | \x5b([^\x0d\x5b-\x5d\x80-\xff]
  | \x5c[\x00-\x7f])*\x5d))*$
///

which reduces exactly to the original regexp. The only changes were removing a pair of extraneous braces around the whole regexp, and the trailing / after the closing $.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • -1 as this is not what he asked. He might very well have decided to only support "normal" mail addresses in his application. This will still be good for most users and good enough for him. – Daniel Hilgarth Apr 12 '13 at 10:31
  • @DanielHilgarth perhaps not, but it's the best advice he's going to get today. – Alnitak Apr 12 '13 at 10:32
  • Still, not an answer. Post it as a comment. Furthermore, I disagree with it being the best advice. See the addition to my first comment. – Daniel Hilgarth Apr 12 '13 at 10:33
  • 1
    I know it's a bad idea in general to do. Every single SO answer on email validation mentions this. If it helps your conscience, then just replace the regexp with some other equally long one. –  Apr 12 '13 at 10:34
  • there is no suitable regex - see http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – Alnitak Apr 12 '13 at 10:38
  • @Alnitak: The OP asks how to execute long regular expressions in javascript / coffeescript, *not* how to validate email addresses. – Daniel Hilgarth Apr 12 '13 at 10:38
  • 1
    I wish I had another really long regexp that had zero to do with emails. Then comments on whether or not to validate emails would appear just as inappropriate as they are now. –  Apr 12 '13 at 10:42
  • @GormCasper ah, I see what you meant by previous comment :) – Alnitak Apr 12 '13 at 10:45
  • I'm actually on your side Alnitak. I totally agree it's a bad idea to do. It's just that it is not what I am asking about. –  Apr 12 '13 at 10:47