.*(\d{3}\-\d{3}\-\d{2}\-\d{2}|\d{3}\-\d{2}\-\d{2}\-\d{3}|\d{10}).*
这个模式运行良好。但突然间它最近停止在 chrome 和歌剧中工作。这里发生了什么 ?这里有什么问题,怎么错的? Opera 正在通知无效转义,在 Chrome 中也是如此。当我在 js 中检查它时,它工作正常。
<form>
<input type="text" pattern=".*(\d{3}\-\d{3}\-\d{2}\-\d{2}|\d{3}\-\d{2}\-\d{2}\-\d{3}|\d{10}).*">
<button>
Send
</button>
</form>
最佳答案
重点是Chrome和Firefox已经支持ES6正则规范,默认支持Unicode模式。
Unicode 模式对于哪些字符可以在模式内转义具有更严格的规则。参见 this reference :
IdentityEscape: In BMP patterns, many characters can be prefixed with a backslash and are interpreted as themselves (for example: if
\u
is not followed by four hexadecimal digits, it is interpreted asu
). In Unicode patterns that only works for the following characters (which frees up\u
for Unicode code point escapes):^
$
\
.
*
+
?
(
)
[
]
{
}
|
同一组字符在 ES6 specs page 中称为 SyntaxCharacter .
因此,您只能在字符类中将 -
转义为特殊字符,并使其成为文字,您可以转义它。在其他任何地方都不能转义。
<form>
<input type="text" pattern=".*(\d{3}-\d{3}-\d{2}-\d{2}|\d{3}-\d{2}-\d{2}-\d{3}|\d{10}).*">
<input type=Submit>
</form>
关于regex - HTML 输入模式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46667891/