Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

Why is this regex allowing a caret?

http://regexr.com/3ars8

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

Should match "17 alphanumeric chars, hyphens allowed too, must include at least one letter and at least one number"

It'll correctly match:

ABCDF31U100027743

and correctly decline to match:

AB$DF31U100027743

(and almost any other non-alphanumeric char)

but will apparently allow:

AB^DF31U100027743
question from:https://stackoverflow.com/questions/65944011/regex-to-find-all-special-charactersjs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...