简单正则表达式的问题
我在验证规则中有以下正则表达式:
^[a-zA-Z0-9',!;?~>+&\"\-@#%*.\s]{1,1000}$
但是,我可以输入 ======
我认为这是不允许的。
我的想法是,如果没有正确转义或其他什么, -
可能会以某种方式造成麻烦,但这超出了我的能力范围。
I have the following regular expression in a validation rule:
^[a-zA-Z0-9',!;?~>+&\"\-@#%*.\s]{1,1000}$
However, I can enter ======
which I believe should not be allowed.
My thoughts is that somehow the -
could cause trouble if not properly escaped or something but this is way over my head.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您向我们展示的带有
-
转义的正则表达式不接受===
。但是如果
-
未转义,< code>=== 将被接受。请参阅此。正则表达式中的
-
是特殊的,如果它没有转义并且被作为范围中的最小值和最大值参与的字符包围,则用作范围运算符:[az]
匹配任何小写字符。[-az]
匹配-
或a
或z
。[az-]
匹配-
或a
或z
。[a\-z]
匹配-
或a
或z
。[acdf]
匹配a
或b
或c
或-
或d
或e
或f
。第一个和最后一个-
充当范围运算符,但中间的一个按字面意思处理。在您的情况下,
=
位于"-@
范围内,因此会匹配。The regex you've shown us with the
-
escaped does not accept===
.But if
-
is not escaped,===
will be accepted. See this.A
-
inside a regex is special and is used as range operator if it's not escaped and is surrounded by characters which participate as min and max in the range:[a-z]
matches any lowercase character.[-az]
matches either a-
ora
orz
.[az-]
matches either a-
ora
orz
.[a\-z]
matches either a-
ora
orz
.[a-c-d-f]
matchesa
orb
orc
or-
ord
ore
orf
. The first and last-
act as range operator but the one in the middle is treated literally.In your case the
=
comes in the range"-@
and hence gets matched.一切都匹配。你想要
matches on everything. You want
-
将被解释为范围指示器。如果要匹配文字-
,则需要将其放在[]
括号内的第一个或最后一个。The
-
will be interpreted as a range indicator. You need to put it either first or last within the[]
brackets if you want to match a literal-
.您的正则表达式对我来说工作正常,但如果我删除
-
的转义,它会匹配=
。我确信你正在这样做。Your regex works fine for me but if I remove the escaping of
-
it matches=
. I'm sure you are doing that.