13

I'm using regex pattern matching for HTML5 form validation. The latest version of Firefox gives me an error. I only started seeing this in Firefox 46. I don't think this was a problem in earlier Firefox versions.

Unable to check <input pattern='[\@\%]'> because the pattern is not a valid regexp: invalid identity escape in regular expression

Caused by this very simple test case:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <form>
    <input pattern="[\@\%]">
  </form>
</html>

Why is escaping these characters considered an error? I've always escaped everything in my regular expressions that isn't a number or a letter. I've never had anything complain this type of escaped character except this version of Firefox.

When I learned regex, I was told that everything that wasn't a number or a letter could have special meaning. Even if it doesn't now, it might in a future version, so it is better to escape them. Is this not true?

Is there a list of characters I shouldn't escape for Firefox?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
  • _sidenote_ :- you don't need to escape those characters in character class as they don't have any special meaning – rock321987 Apr 30 '16 at 10:38

1 Answers1

12

This is due to the following change: Bug 1227906 - HTML pattern attribute should set u flag for regular expressions

As someone has already said, you don't have to escape those characters. Just use:

<input pattern="[@%]">
Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
  • Posted the [site compatibility doc](https://www.fxsitecompat.com/en-CA/docs/2016/input-pattern-now-sets-u-flag-for-regular-expressions/) for this. – Kohei Yoshino Apr 30 '16 at 16:30
  • The [related github issue](https://github.com/whatwg/html/issues/439) has also now been reopened. – sideshowbarker May 01 '16 at 15:55
  • Any idea why you can't escape those characters in Unicode regex mode? – Stephen Ostermiller May 01 '16 at 19:26
  • 2
    @StephenOstermiller It’s [not necessary](https://mathiasbynens.be/notes/es6-unicode-regex#impact-syntax) to escape them, so why would you? The reason unnecessary escapes throw in `u` mode is so that the spec can later assign a special meaning to some of them, such as `\p` and `\P`. That would be a backwards-incompatible change if `\p` were equivalent to just `p`. – Mathias Bynens May 02 '16 at 06:25
  • Escaped numbers and letters are reserved for special use, but my understanding is that all symbols are reserved for future use unescaped. – Stephen Ostermiller May 02 '16 at 16:37
  • One reason to add extra escapes is because simple regex escaping utilities add it, and to date that has been safe. – Brett Zamir Mar 12 '20 at 07:12