-2

disclaimer: entirely new to regexes

I am trying to write a simple regex to validate email strings like this:

/^\w+@\w+\.\w{1,4}/.test(emailstring);

The above should return false for = a@d.abcde but true for a@d.abcd. I need the extension to be limited to four characters {1,4}. But it always returns true for any length of tld extension. What's wrong in the above expression?

beNerd
  • 3,314
  • 6
  • 54
  • 92

1 Answers1

3

Add the end-of-line anchor ($):

/^\w+@\w+\.\w{1,4}$/.test(emailstring);
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40