[英]Html regex pattern: [\d\s-]{3} works but [\d-\s]{3} doesn't. Why?
Codepen example: Codepen示例:
https://codepen.io/Trost/pen/KXBRbY https://codepen.io/Trost/pen/KXBRbY
Try putting 1 symbol in both fields. 尝试在两个字段中都放入1个符号。
I can't get what's wrong. 我不明白怎么了。 If I test these regex in https://regex101.com , they appear to be identical.
如果我在https://regex101.com中测试这些正则表达式,它们似乎是相同的。
<form>
Works: <input type="text" name="country_code" pattern="[\d\s-]{3}" title="-23" required>
<input type="submit">
</form>
<form>
Bug: <input type="text" name="country_code" pattern="[\d-\s]{3}" title="- 3" required>
<input type="submit">
</form>
You define two different things: 您定义了两个不同的东西:
[az]
is a definition of a range - all characters from a
to z
. [az]
是范围的定义-从a
到z
所有字符。 [az-]
is a definition of a set of three elements - a
, z
and -
. [az-]
是一组三个要素的定义- a
, z
和-
The real root cause here is that the regex [\\d-\\s]
is used in the pattern
HTML5 attribute, and in the latest versions of Chrome and FireFox is compiled as an ES2015-compatible regex with the u
modifier. 真正的根本原因是在
pattern
HTML5属性中使用了正则表达式[\\d-\\s]
,并且在最新版本的Chrome和FireFox中,使用u
修饰符将其编译为与ES2015兼容的正则表达式。 The consequence is that there are much stricter escaping rules for the Unicode regex patterns. 结果是Unicode正则表达式模式有更严格的转义规则 。
What it means is whenever a char cannot be parsed unambiguously, it is an error. 这意味着每当无法明确解析char时,这就是一个错误。 When a char is escaped, but does not need escaping, it is again an error.
当一个字符被转义但不需要转义时,它又是一个错误。
The chars that you may escape in the character class inside a u
based regex are +
, $
, ^
, *
, (
, )
, |
在基于
u
的正则表达式中的字符类中可能会逸出的字符是+
, $
, ^
, *
, (
, )
和|
, \\
, [
, ]
, .
,
\\
, [
, ]
, .
, ?
,
?
, -
, {
, }
(see this source ). ,
-
, {
, }
(请参见此源 )。 If the -
is at the start/end of the character class, it still can go unescaped, as it can only be parsed as a literal hyphen there. 如果
-
位于字符类的开头/结尾,则它仍可以不转义,因为它只能在此处解析为文字连字符。
In between two shorthand character classes, an unescaped -
will produce an error because it is treated as a user error. 在两个速记字符类之间,未转义的
-
将产生错误,因为它被视为用户错误。
So, either place a hyphen at the start/end (it is always the best option), or escape it inside the character class (and never escape it outside of the character class). 因此,可以在开始/结尾处放置一个连字符(始终是最佳选择),或者在字符类内部将其转义(并且永远不要在字符类外部进行转义)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.