Don Diego
Don Diego

Reputation: 1488

Regexp with a maximum of 2 consecutive equal chars and other options

I'm totally new with regualr expressions and I have to build one with the following requisites:

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

Answers (1)

The fourth bird
The fourth bird

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

Related Questions