Password check using Regex

Tell us what’s happening:
Good day campers
i want my code to return true regardless of the position were lowercase letter is placed.
currently, the code below returns true but if the lower case letter is placed before the last 3 uppercase letter, it returns false. please i need a comprehensive explanation of what is happening.

Thank you

Your code so far


let password = "YHAFASDtAFSDA";
let myRegex = /(?=.{6,})(?=.[a-z]+)(?=[A-Z]+)/
let result = myRegex.test(password)

console.log(result)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36.

Challenge: Positive and Negative Lookahead

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead

you can use character class [a-zA-Z] or you can use \w (which equals [a-zA-Z_] or you can use i flag (ignore case)

the condition to the password are:
-at least one lowercase
-at least one uppercase
-six or more characters.
I just want to know whats wrong with my code as i want the lowercase to be in any position except the front

i need explanation to what is wrong with my code when i place a lowercase letter before the last 3 uppercase letter

let password = "YHAFASDAFhSDA";
let myRegex = /^(?=.{6,})(?=.[a-z])(?=[A-Z]+)$/
let result = myRegex.test(password)
console.log(result)

my code:

let password = "YHAFASDAFhSDA";
let myRegex = /(?=.{6,})(?=.[a-z])(?=[A-Z]+)/
let result = myRegex.test(password)
console.log(result)

it returns “false”

this returns ‘true’

let password = "YHAFASDAhFSDA";
let myRegex = /(?=.{6,})(?=.[a-z])(?=[A-Z]+)/
let result = myRegex.test(password)
console.log(result)

all three lookaheads look from the same position, so you need to write them considering that

please can you explain further using each lookaheads in my code. thanks

the first one says “at least six characters”, the second one say “one character then one lowercase letter” (so second position must be lower case letter), third one say “one or more upper case letters”
the only thing that pass is a string of at least 6 characters with an upper case letter at index 0 and a lower case letter at index 1

you can use something like https://regex101.com/ to test your regex

but if if the second says “one character then one lowercase letter”, why do i have a match in the code below when the lowercase letter is before the last 4 uppercase letter.

let password = "VHJVJJBJBJBVGHsVHJV";
let myRegex = /(?=.{6,})(?=.[a-z])(?=[A-Z]+)/
let result = myRegex.test(password)

true

but returns “false” when the lowercase is before the last 3 uppercase letter

let password = "VHJVJJBJBJBVGHVsHJV";
let myRegex = /(?=.{6,})(?=.[a-z])(?=[A-Z]+)/
let result = myRegex.test(password)

false 

There are two things to bear in mind here:

  1. After each lookahead completes, the search index returns to the value it was at the start of the lookahead. Since your regex only consists of lookaheads, they will all start their matching from the SAME index.

  2. Your regex does not enforce any starting position from which matching should start. Therefore it can start from ANYWHERE.

This means that if all the lookaheads can successfully complete their matching starting from any mutual index in the string, the test function will return true.

In the case of "VHJVJJBJBJBVGHsVHJV", the test function returns true as all lookaheads can successfully complete their matching if they all start from index 13 (the position just before "s").

In the case of "VHJVJJBJBJBVGHVsHJV", there is no index from which all lookaheads can successfully complete their matchings. Therefore the test function returns false.

Thank you so much. it’s clear to me now. The code below work fine

let password = "VHJVJJBJBJBVGHVsHJV";
let myRegex = /(?=.{6,})(?=.*[a-z]+)(?=.*[A-Z]+)/
let result = myRegex.test(password)