Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explaining password regex component by component (javascript) [closed]

/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,8}/

This RegEx is supposed to validate a password, which should contain at least one digit, both lowercase and uppercase characters. Can anyone explain this RegEx by smaller components?

like image 644
emen Avatar asked Nov 25 '11 11:11

emen


2 Answers

/(?=.\d)(?=.[a-z])(?=.*[A-Z]).{6,8}/

This regex is generally used to validate password, i.e.

password should contain 1 UpperCase,1 LowerCase and 1 numeric and no special characters.

(?=.*\d) //at least 1 numeric charater.

(?=.*[a-z]) //atleast 1 lowercase.

(?=.*[A-Z]) //atleast 1 uppercase.

.{6,8} //string is of 6 to 8 length.

Hope this helps.

like image 122
AlphaMale Avatar answered Nov 07 '22 19:11

AlphaMale


(?=.*\d) ensures your string has a digit in it.

(?=.*[a-z])ensures your string has a lowercase ASCII letter in it.

(?=.*[A-Z])ensures your string has a uppercase ASCII letter in it.

.{6,8} matches a string of atleast 6 and atmost 8 characters.

Since the anchors are missing, your regex would match any string which has as its substring a string that satisfies all of the above 4 conditions.

like image 45
codaddict Avatar answered Nov 07 '22 18:11

codaddict