0

So I am trying to use the following regex in an HTML input:

<input ... pattern="^[a-zA-Z0-9\-.,&?!@#~\';+_ ]+$" ... >

Yes, it's a pretty basic way of setting up a whitelist, and yes, it isn't exactly pretty to look at. However, at the very least it works on regex101.com, so I figured it would be fine to use. Unfortunately, I get the following error:

Pattern attribute value ^[a-zA-Z0-9-.,&?!@#~\';+_ ]+$ is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /^[a-zA-Z0-9-.,&?!@#~\';+_ ]+$/: Invalid escape

How am I able to check so that what is acceptable on a website like regex101.com matches what is acceptable in an HTML page?

Toto
  • 89,455
  • 62
  • 89
  • 125
Alexander
  • 999
  • 1
  • 9
  • 18
  • To figure a literal backslash, you need to escape it (with an other backslash). You get this error message because the escape sequence `\'` doesn't exist. Also you can avoid to escape the hyphen if you put it at the end of the character class. To finish, you don't have to put anchors `^` and `$` in a pattern attribute because they are implicit. – Casimir et Hippolyte Jul 16 '17 at 18:25
  • So, does the error come from the _engine_ or does it come from the _parser_ ? –  Jul 17 '17 at 00:29

0 Answers0