Why is this regex allowing a caret?

前端 未结 3 1848
鱼传尺愫
鱼传尺愫 2020-11-21 05:34

http://regexr.com/3ars8

^(?=.*[0-9])(?=.*[A-z])[0-9A-z-]{17}$

Should match \"17 alphanumeric chars, hyphens allowed too, must include at le

3条回答
  •  长情又很酷
    2020-11-21 06:22

    Because your character class [A-z] matches this symbol.

    [A-z] matches [, \, ], ^, _, `, and the English letters.

    Actually, it is a common mistake. You should use [a-zA-Z] instead to only allow English letters.

    Here is a visualization from Expresso, showing what the range [A-z] actually covers:

    screenshot from Expresso showing the ASCII table, where you can see what the [A-z] range actually covers

    So, this regex (with i option) won't capture your string.

    ^(?=.*[0-9])(?=.*[a-z])[0-9a-z-]{17}$
    

    In my opinion, it is always safer to use Ignorecase option to avoid such an issue and shorten the regex.

提交回复
热议问题