TLDR; I breakdown your code piece by piece to help you see what it is doing, why it is not giving you the expected results, and why it isn’t passing all the FCC code check tests.
So, getting back to your actual code…
Doing a “Lookback” (lame pun intended ) at the Lookahead challenge question, it says:
“Use lookaheads in the pwRegex
to match passwords that are greater than 5 characters long, and have two consecutive digits.”
(emphasis mine)
So the challenge wants you to match a pattern of more than 5 characters (which can be any character) followed by 2 consecutive digit characters (0-9).
As @twotani pointed out, do you want 5 or more (at least 5), or explicitly more than 5 characters followed by 2 digits? The difference between “or more” and “more than” is subtle but there is a difference.
If you don’t already have a good reference for the RegEx syntax, this is a good resource:
w3schools - JavaScript RegExp Reference
Let’s examine and break down what your pattern is saying…
The first part:
?=\w{5,}
is saying: Find 5 or more word characters (a-z,A-Z,0-9).
\w
is word characters.
{5,}
is a sequence of at least 5 (or more).
The second part:
?=\D*\d{2}
is saying: any string that contains zero or more occurrences of a non-digit character followed by 2 digit characters.
\D*
is zero or more occurrences of non-digit characters (excludes 0-9).
It’s OK if the preceding characters are a-z, A-Z, 0-9 (\w* word characters).
\D*
should work though, you are just being more specific here than it requires.
\d
is a digit character (0-9).
{2}
is a sequence of 2.
so a digit character followed by another digit character (2 consecutive digit characters).
So altogether, your code is saying:
Find at least 5 word characters
then find 2 consecutive digit characters
preceded by zero or more non-digit characters.
Which is very close to, but not exactly what the lesson is asking you to do.
As I mentioned in my previous reply, if test()
returns false
then you know your RegEx pattern didn’t find a match, but if that is the expected result, it will pass the FCC code check.
try changing:
let sampleWord = "astronaut";
to :
let sampleWord = "12345";
and then add a line at the very bottom of all the code that says:
console.log(result);
When you click “Run the Tests”, the console should output:
// running tests
Your regex should not match the string 12345
true
// tests completed
// console output
true
Which proves your RegEx matches on 12345
and returns true
. If it was a working solution, it should not match on 12345
and should return false
.
I hope all that helps & makes sense.