Simple regex problem!

Question:
Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long and have two consecutive digits.

Answer:
let sampleWord = “astronaut”;
let pwRegex = /(?=.{5,})(?=\D*\d{2})/; // Change this line
let result = pwRegex.test(sampleWord);

I’m a little bit confused about it. How I’m supposed to read this regex?

the logic is, the string must match this rule (?=.{5,}) AND this rule (?=\D*\d{2})? (the doubt is about the logical operator OR, AND, etc…)

Another question how exactly the parenthesis work?

Thanks!

Thing one: Please, use MarkDown to enhace code visualitation of you regex.

Thing two:

?= //that is lookaheads
. //just any character,
{5} //five characters,
() //group of characters

Thing three: You can read the rest of the regex yourself. Overcoming the challenges of freeCodeCamp will help you master your regex skills.

If you want a quick solution, check this documentation:

1 Like

Yes, there is an implied AND between those two lookaheads. For pwRegex.test(sampleWord); to return true there must be some single place in that string where both lookaheads match.

1 Like