正则表达式在Chrome模式输入验证中 [英] Regex on Chrome pattern input validation
问题描述
我有一个符合RFC 5322的电子邮件正则表达式模式,以验证我的输入. 这在所有浏览器中都适用,但是Chrome在控制台上向我显示了以下错误:
I have an RFC 5322 compliant email regex pattern to validate my input. This works in all browsers, but Chrome show me the following error on console:
Pattern attribute value ^[-a-z0-9~!$%^&*_=+}
{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_]
[-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz
|com|coop|edu|gov|info|int|mil|museum|name
|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]
{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))
(:[0-9]{1,5})?$
is not a valid regular expression:
Uncaught SyntaxError: Invalid regular expression:
/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^
&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z
0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov
|info|int|mil|museum|name|net|org|pro|travel
|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.
[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/
Invalid escape
我只是找不到哪个块具有此无效的转义.
I just can't find what chunk have this invalid escape.
推荐答案
由于Chrome的pattern
属性会自动添加u
修饰符,因此将应用更严格的regex语法规则.基本上,您不能转义任意符号.如果该符号不是特殊的正则表达式元字符,则无法将其转义,否则会出现此错误.
Since Chrome pattern
attribute automatically adds u
modifier, stricter rules for regex syntax are applied. Basically, you cannot escape arbitrary symbols. If the symbol is not a special regex metacharacter, you cannot escape it, or you will get this error.
因此,在您的情况下,单个撇号一定不能转义.删除转义的反斜杠之前,该正则表达式将在每个浏览器中再次运行.
So, in your case, the single apostrophe must not be escaped. Remove the escaping backslash before it, and the regex will work in every browser again.
这篇关于正则表达式在Chrome模式输入验证中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!