Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox error: Unable to check input because the pattern is not a valid regexp: invalid identity escape in regular expression

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?

like image 580
Stephen Ostermiller Avatar asked Apr 30 '16 10:04

Stephen Ostermiller


1 Answers

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="[@%]">
like image 137
Kohei Yoshino Avatar answered Sep 28 '22 09:09

Kohei Yoshino