开发者

Problem with simple regex

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.


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 - or a or z.

[az-] matches either a - or a or z.

[a\-z] matches either a - or a or z.

[a-c-d-f] matches a or b or c or - or d or e or f. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜