.*(\d{3}\-\d{3}\-\d{2}\-\d{2}|\d{3}\-\d{2}\-\d{2}\-\d{3}|\d{10}).*
这种模式运行良好。但最近它突然停止在 Chrome 和 Opera 中工作。这里发生了什么 ?这里有什么问题以及它是如何错误的? 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/39895209/