-1

I have a regex query for password verification, the rules are password must be between 8-15 chars, 1number + 1 special characters. It is working perfectly in web form.

I only need to understand it fully. If anyone can help me in describing this regex group by group,, it will be of great help to me. I do understand some part but not all.

^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{7,15}$
raju
  • 4,859
  • 11
  • 54
  • 108
  • The regex shown doesn't do what you say it does. It also requires at least one lowercase letter and one uppercase letter. Have a look at [a regex guide](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions) and read about lookaheads. – nnnnnn Jul 18 '17 at 03:09
  • There's a cool site for regex testing: [regex101](https://regex101.com/r/vsNNwO/1). You can see 'Explanation' on the right. – AlexM Jul 18 '17 at 03:13
  • Sorry, I updated the regex. – raju Jul 18 '17 at 03:15
  • @raju now it seem to do what you described, except that the length is 7-15. So what is your question? – AlexM Jul 18 '17 at 03:18
  • @AlexM, I could not understand the second group , (?=.*[!@#$%^&*]). What it actually does. – raju Jul 18 '17 at 03:19
  • It's `posivite lookahead` for presence of any character from `!`, `@`, `#`, `$`, `%`, `^`, `&`, `*` – AlexM Jul 18 '17 at 03:24

1 Answers1

1

Since you updated the regex...

^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{7,15}$

^(?=.*[0-9]) from the start of the string, match any numbers. The lookahead ?= prevents the regex from continuing if nothing matches.

(?=.*[!@#$%^&*]) match any special characters in the group.

[a-zA-Z0-9!@#$%^&*] capture all letters, numbers, and special characters. At least 7 and up to 15 until the end of the line.

Bricky
  • 1,879
  • 9
  • 23