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

≯℡__Kan透↙ 提交于 2019-12-17 20:07:08

问题


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:


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="[@%]">


来源:https://stackoverflow.com/questions/36953775/firefox-error-unable-to-check-input-because-the-pattern-is-not-a-valid-regexp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!