Regular Expressions - Positive and Negative Lookahead

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

Shouldnt this first positive lookaround be: /(?=\w{6,}) instead of /(?=\w{6})?

Not using the comma means that we would be looking for passwords exactly 6 characters long, correct? The task says to match passwords greater than 5 characters long, which would include 6, 7, 8 etc…, correct? Including the comma would include numbers greater than 6 and not just the number 6, correct?

let sampleWord = "astronaut";
let pwRegex = /(?=\w{6})(?=\w*\d{2})/; // Change this line
let result = pwRegex.test(sampleWord);
console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0

Challenge: Regular Expressions - Positive and Negative Lookahead

Link to the challenge:

Once you have matched six characters, there is no need to match more. That is enough to determine that there are at least six, and you do not need to return the contents of the string, so it does not matter what comes after.

In fact, if your regular expression is made up entirely of lookaheads, the matched string will be empty:

let sampleWord = "bana12";
[...]

let result = pwRegex.test(sampleWord);
console.log(result);
// true

let match = JSON.stringify(sampleWord.match(pwRegex));
console.log(`Matched string(s): ${match}`);
// Matched string(s): [""]

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.