0

Unable to check <input pattern='/^[a-zA-Zà-úÀ-Ú0-9äöüÄÖÜß@\s+-_.,{}[]()#']+$/'> because the pattern is not a valid regexp: invalid identity escape in regular expression

First Try: <br />
<input type="text" pattern="/^[a-zA-Zà-úÀ-Ú0-9äöüÄÖÜß@\s\+\-\_\.\,\{\}\[\]\(\)\#\']+$/">
<br />
Second Try: <br />
<input type="text" pattern="[a-zA-Zà-úÀ-Ú0-9äöüÄÖÜß@\s\+\-\_\.\,\{\}\[\]\(\)\#\']+">
ar099968
  • 6,963
  • 12
  • 64
  • 127
  • Does this answer your question? [invalid escape in pattern HTML/Javascript](https://stackoverflow.com/questions/41409872/invalid-escape-in-pattern-html-javascript) – Oskar Grosser Mar 03 '23 at 16:55

1 Answers1

1

Quoting MDN on the pattern attribute:

... No forward slashes should be specified around the pattern text.

The forward slashes are JavaScript syntax for regular expression literals.

Quoting from the same page:

... the 'u' flag is specified when compiling the regular expression, so that the pattern is treated as a sequence of Unicode code points, instead of as ASCII.

And quoting MDN's Guide Regular expressions:

  • Unicode regular expressions do not support so-called "identity escapes"; that is, patterns where an escaping backslash is not needed and effectively ignored. ...

This means only required character escapes are valid.

You should also be aware that ... (quoting from the same page)

  • The - character is interpreted differently within character classes. In particular, for Unicode regular expressions, - is interpreted as a literal - (and not as part of a range) only if it appears at the start or end of the character class.

In conclusion, your first pattern in fixed could look like this:

<input type="text" pattern="^[a-zA-Zà-úÀ-Ú0-9äöüÄÖÜß@\s+\-_\.,\{\}\[\]\(\)#']+$">
Oskar Grosser
  • 2,804
  • 1
  • 7
  • 18