Firefox 错误:无法检查输入,因为模式不是有效的正则表达式:正则表达式中的身份转义无效 [英] Firefox error: Unable to check input because the pattern is not a valid regexp: invalid identity escape in regular expression
问题描述
我正在使用正则表达式模式匹配进行 HTML5 表单验证.最新版本的 Firefox 给了我一个错误.我只是在 Firefox 46 中才开始看到这个.我认为这在早期的 Firefox 版本中不是问题.
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>
为什么转义这些字符会被视为错误?我总是在我的正则表达式中对不是数字或字母的所有内容进行转义.除了这个版本的 Firefox,我从来没有抱怨过这种类型的转义字符.
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?
是否有我不应该为 Firefox 转义的字符列表?
Is there a list of characters I shouldn't escape for Firefox?
推荐答案
这是由于以下更改:Bug 1227906 - HTML pattern
属性应该为正则表达式设置 u
标志
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="[@%]">
这篇关于Firefox 错误:无法检查输入,因为模式不是有效的正则表达式:正则表达式中的身份转义无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!