2

I have an RFC 5322 compliant email regex pattern to validate my input. This works in all browsers, but Chrome show me the following error on console:

Pattern attribute value ^[-a-z0-9~!$%^&*_=+}
{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_]
[-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz
|com|coop|edu|gov|info|int|mil|museum|name
|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]
{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))
(:[0-9]{1,5})?$
is not a valid regular expression:

Uncaught SyntaxError: Invalid regular expression:
/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^
&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z
0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov
|info|int|mil|museum|name|net|org|pro|travel
|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.
[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/

Invalid escape

I just can't find what chunk have this invalid escape.

dbugger
  • 15,868
  • 9
  • 31
  • 33
Douglas
  • 23
  • 3

2 Answers2

2

The latest Chrome (at least from 114) actually uses the /v flag now. That requires that you escape all special characters within character classes.

So for example, to match { or } you should write [\{\}] instead of [{}].

gitaarik
  • 42,736
  • 12
  • 98
  • 105
0

Since Chrome pattern attribute automatically adds u modifier, stricter rules for regex syntax are applied. Basically, you cannot escape arbitrary symbols. If the symbol is not a special regex metacharacter, you cannot escape it, or you will get this error.

So, in your case, the single apostrophe must not be escaped. Remove the escaping backslash before it, and the regex will work in every browser again.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563