1

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
Don Diego
  • 1,309
  • 3
  • 23
  • 46
  • Looks like password validation. You may benefit from [Reference - Password Validation](https://stackoverflow.com/questions/48345922/reference-password-validation/) – ctwheels Mar 22 '18 at 16:07
  • 1
    It will help everyone to help you more effectively if you post a few lines of your test data – SaganRitual Mar 22 '18 at 16:07
  • Put your negative lookahead before the `(.{8,15})` portion. Remove `{2}` and remove the `()` around the whole expression or change `\1` to `\2` – ctwheels Mar 22 '18 at 16:08
  • thanks for the link, it seems very interesting...unfortunately, i have to do that way; I tried putting the `(?!.*(.)\1{2})` before the `(.{8,15})` but it does not work at all, neither the previous rules – Don Diego Mar 22 '18 at 16:13
  • @GreatBigBore it's pretty simple: `if (regexp.test(password)) { console.log("ok!"); } else { console.log("no..."); }` – Don Diego Mar 22 '18 at 16:14
  • @Diego I mean the data that you're trying to match. – SaganRitual Mar 22 '18 at 16:15
  • @GreatBigBore is suggesting sample data which you assert should pass. –  Mar 22 '18 at 16:15
  • Following @ctwheels advice leads to `/^(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z])(?!.*(.)\1{2})(.{8,15})$/`, and that does matches as desired. Note that one of your 'working' examples shoudn't work, as it doesn't contain an uppercase letter.... – Tom Regner Mar 22 '18 at 16:28
  • Are you sure this is the actual regex and the actual test data you're using? I ask because when I run what you posted, and change this `asdfghjkl11` to `asdfghjkl11Q` (per your capital letter requirement), the "WORKING" section works perfectly, just as you have specified. – SaganRitual Mar 22 '18 at 16:49
  • And have you studied the link that @ctwheels posted? – SaganRitual Mar 22 '18 at 17:01
  • @Diego Question though, why should `asdfghjkl11` match? This does not contain a capital letter and why should `asdfghjkl1QQq` not match? – The fourth bird Mar 22 '18 at 18:46

1 Answers1

2

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}).+$

The fourth bird
  • 154,723
  • 16
  • 55
  • 70