Regular Expressions: Positive and Negative Lookahead - Instructions vs Hints

Tell us what’s happening:
Hello! I am confused comparing the instructions to the hints for this exercise.

Instructions state:
“Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, do not begin with numbers, and have two consecutive digits.”

However the hints state:
“A second lookahead must be used to check for consecutive numerical values at the end of the string.”

and

“Remember to specify the exact amount of numbers you want to appear at the end of the string.”

So it seems to me that the instructions are asking us to lookahead for two consecutive digits anywhere except the first character in the string

yet the hints are stating that we must specify the numerical values we want to appear at the end of the string.

It seems the instructions and the hints are providing conflicting info, so if someone can please clarify that they are not conflicting and explain why they are not, it might help me better understand how to solve the challenge.

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36.

Challenge: Positive and Negative Lookahead

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead

when in doubt, look at the tests:

Your regex should match "astr1on11aut"

The challenge could have been changed since last time that the guide was updated, so pick what’s written there always with a grain of salt

there is the length limit, then it can’t start with a number and there must be at least too consecutive numbers anywhere else in the string

@ILM - do you mean I should disregard the hints, or instructions, or both? (in the case of both I would be even more confused, so I hope that’s not the case!). As you probably know by now, I find the instructions exasperating at times.

Here is the code I have now:

let sampleWord = "astronaut";

let pwRegex = /(?=\w{6,})(?=\D+\d{2})/; // Change this line

let result = pwRegex.test(sampleWord); 

I don’t know why it doesn’t work for pw 8pass99

I never said “disregard”, I said you should take the articles in the guide with a pinch of salt as you can’t know if they were updated since last challenge update - there are many guide articles and the challenges have often minor adjustments.

I just said that if you have doubts, check the tests and see which interpretation correspond to tests behaviour.

For your regex, you may want to specify where to start the test, as right now there are no specifications, and the substring pass99 is matched by your regex, so the test method returns true

I’m not sure what you mean by saying that I should take the articles in the guide with a pinch of salt - are you referring to the hints and solutions?

Can you please clarify what you mean by “if you have doubts, check the tests and see which interpretation correspond to tests behaviour” and “you may want to specify where to start the test?”

thanks for the help!

you are asking if numbers must be only at the end - but astr1on11aut must pass the test, so no, they can be anywhere in the string but at the beginning.

the test method searches for a substring that matches the pattern, if it finds it returns true.
8pass99 has the substring pass99 that matches the pattern, so it returns true.
You need to specify that the substring must start at the beginning of the string to avoid this.

those are guide articles, you find them in the Guide subforum.
It happens that the guide get it of sync with the challenges, keeping everything up to date is a lot of work.

I’m not sure what you mean by the substring must start at the beginning of the string. I think that I am not quite understanding how this challenge works. Anyway, I appreciate the help.

the test method returns true or false based on if it finds the pattern in the string, or not.

in this case you need to make so that strings that have these requisites make the test method return true: longer than 5 characters, can’t start with a number, there are at least two consecutive numbers in the string

the test method will check for a substring in the string that matches the pattern.

if the substring that matches the pattern start in the middle of the string than the condition of “can’t start with a number” can’t be confirmed, so you need to make sure the pattern will match only from the start of the string.

your first lookahead will look for at least 6 characters - string length condition satisfied
second lookahead:
\D* means “0 or more non number characters” - there must be at least one non number at the beginning
\d{2} - 2 consecutive numbers. This is correct.

OK, thank you for the further explanation!