Reputation: 1
Could someone explain to me why the following regex
^(?=.*\w)(?=.*[!@#$*_])(?!.*\s).{8,20}$
match : test*~|-*test
I'm trying to validate the following rules:
!@#$*_
Upvotes: 0
Views: 54
Reputation: 117
Your pattern requires
(1) (?=.*\w) at least one word char
(2) (?=.[!@#$_]) at least one of those special chars
(3) (?!.*\s) no space char
(4) .{8,20} any char, from 8 to 20 long
Your input test*~|-*test matches the pattern
For what you're after I'd use
^(?=.*[!@#$*_])(?=.*\w)[\w!@#$*_]{8,20}$
(1) (?=.[!@#$_]) at least one special char
(2) (?=.*\w) at least one word char
(3) [\w!@#$*_]{8,20} word char or special char, 8 to 20 times
Upvotes: 1