14

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?

1
  • sidenote :- you don't need to escape those characters in character class as they don't have any special meaning Commented Apr 30, 2016 at 10:38

1 Answer 1

13

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="[@%]">
Sign up to request clarification or add additional context in comments.

6 Comments

Posted the site compatibility doc for this.
The related github issue has also now been reopened.
Any idea why you can't escape those characters in Unicode regex mode?
@StephenOstermiller It’s not necessary 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.
Escaped numbers and letters are reserved for special use, but my understanding is that all symbols are reserved for future use unescaped.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.