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 @ least 1 letter , @ least 1 number"

it'll correctly match:

abcdf31u100027743 

and correctly decline match:

ab$df31u100027743 

(and other non-alphanumeric char)

but apparently allow:

ab^df31u100027743 

because character class [a-z] matches symbol.

[a-z] matches [, \, ], ^, _, ` , english letters.

actually, common mistake. should use [a-za-z] instead allow english letters.

here visualization expresso, showing range [a-z] covers:

screenshot expresso showing ascii table, can see [a-z] range covers

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

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

in opinion, safer use ignorecase option avoid such issue , shorten regex.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -