0

I'm trying to get my email validation regex working but the first group isn't allowing me to use the . after the \d for some reason. Am I doing something wrong? I am very new to handling regex but I believe you cannot just do \d.- as the . has a different meaning in regex.

const patterns = { email: /^([a-z\d\.-]+)@([a-z\d-]+)\.([a-z]{2,8})(\.[a-z]{2,8})?$/ };
halfer
  • 19,824
  • 17
  • 99
  • 186
Chris Marshall
  • 740
  • 1
  • 9
  • 25
  • I believe this is eslint actually just trying to be more useful than its being as far as im aware this is doing what I want it to do. If not any input would be amazing – Chris Marshall Oct 26 '19 at 22:07
  • Fyi, a dot in a character class is just a dot. So `[.]` is equivalent to `\.` – LukStorms Oct 26 '19 at 22:15
  • Perhaps just simplify the regex : `/^([\w.-]+)@([\w.-]+)\.(\w{2,8})$/` – LukStorms Oct 26 '19 at 22:24
  • /^([\w.-]+)@([\w.-]+)\.(\w{2,8})$/ Will the last ground need to be there? as far as i knew the ? had to be after the last group as there might be a need for a .com .uk ect – Chris Marshall Oct 27 '19 at 00:22
  • Then would this validation actually be doing something with the capture groups afterwards? And is there a difference when the optional group is placed before? F.e. `^\S+@\S+(\.\w+)?\.\w+$` versus `^\S+@\S+\.\w+(\.\w+)?$` – LukStorms Oct 27 '19 at 07:35
  • Btw, when it's about e-mail validation. You might find [this](https://stackoverflow.com/q/46155/4003419) old SO post an interesting read. – LukStorms Oct 27 '19 at 07:59
  • Please, have a look at these sites: [TLD list](https://www.iana.org/domains/root/db); [valid/invalid addresses](https://en.wikipedia.org/wiki/Email_address#Examples); [regex for RFC822 email address](http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html) – Toto Oct 27 '19 at 09:40

0 Answers0