Simple regex problem!

Simple regex problem!
0

#1

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!


#2

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.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions

If you want a quick solution, check this documentation:


#3

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.