0

I have following regex string to check for valid email formats

/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

At the very end of it I want to add unicode flag u so it will look like this

/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/u

However I am getting error saying that regex becomes invalid with unicode flag. Is there any possibility to set it here?

Ilja
  • 44,142
  • 92
  • 275
  • 498
  • Why do you add `u` flag? The reason for the error is the escaped hyphen. You do not need to add `u` modifier here. – Wiktor Stribiżew Mar 09 '19 at 15:56
  • 2
    Either you're using the "official" regular expression stated in [RFC822](http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html) or you test with something easy like `/\S+@\S+/` and then send a verification mail to the given address. That's the only reliable way to test if a mail address is valid (and that it actually exists). – Andreas Mar 09 '19 at 15:57

1 Answers1

1

There are multiple solutions in order to validate unicode characters, but this flag cannot be used like that. The \u flag is most used to be followed by a char code like \u00C0.

I think the most reliable solution is to specify the range of accepted unicode characters in the regex.

Something like this should work:

/^(?!\.)((?!.*\.{2})[a-zA-Z0-9\u00E0-\u00FC.!#$%&'*+-/=?^_`{|}~\-\d]+)@(?!\.)([a-zA-Z0-9\u00E0-\u00FC\-\.\d]+)((\.([a-zA-Z]){2,63})+)$/

The solution applied here is to support characters from à to ü.

Regex tester: https://www.regexpal.com/?fam=108260

Related question for mathching unicode characters: Matching accented characters with Javascript regexes

piraces
  • 1,320
  • 2
  • 18
  • 42