Reputation: 1488
I'm totally new with regualr expressions and I have to build one with the following requisites:
between 8 and 15 chars
at least 1 alphabetic char (a-z,A-Z)
at least 1 non alphabetic (all the others)
at least 1 CAPITAL letter
at least 1 non-capital letter
maximum of 2 consecutive equal chars (e.g.: 'g' accepted, 'gg' accepted, 'ggg' not)
I tried with this one, but it works only with a maximum of 5 consecutive equal chars (dont understand why). What I'm doing wrong?
var regexp = /^((?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z])(.{8,15})(?!.*(.)\1{2}))$/;
EDIT it works with
asdfghjkl1Q
asdfghjkl1QQQ
asdfghjkl1QQQQQ
it not works with
asdfghjkl1QQQQQQ
asdfghjkl1QQQQQq
what i'm trying to obtain is: WORKING with :
asdfghjkl1Q
asdfghjkl1QQ
asdfghjkl11
NOT WORKING with:
asdfghjkl1QQQ
asdfghjkl1QQq
asdfghjkl111
Upvotes: 1
Views: 118
Reputation: 163267
I think you don't need the outer capturing group so you might omit it.
You could first check for the 8,15 characters until the end of the string $
using a lookahead (?=.{8,15}$)
If all the lookaheads match, then match any character one or more times .+
Try it like this:
^(?=.{8,15}$)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z])(?!.*(.)\1{2}).+$
Upvotes: 2