2

I found this simple regexp (i know it's probably not perfect) somewhere online to validate an email address.

/^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/

The problem is, that this regexp doesn't allow for the following case:

myname@test-domain.com
my-name@test-domain.com

Any ideas?

ps. I'm using this regexp within javascript.

matt
  • 42,713
  • 103
  • 264
  • 397

1 Answers1

5

If you simply want to add hyphens you can change the regexp to:

/^(?:\w+[\-\.])*\w+@(?:\w+[\-\.])*\w+\.\w+$/

To add other special chars e.g. like underscore just put them in the first (not the second) pair of square brackets, i.e. change [\-\.] to [\-\._].

Also have a look on this question and its anwer.

Community
  • 1
  • 1
speakr
  • 4,141
  • 1
  • 22
  • 28