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:
FelipeMacenaAlves:
(?=.{5,})
?= //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.
Learn to Code — For Free
If you want a quick solution, check this documentation:
Regular expressions are patterns used to match character combinations in strings.
In JavaScript, regular expressions are also objects. These patterns are used with the exec() and test() methods of RegExp, and with the match(), matchAll(),...
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