Need some clarification on Positive and Negative Lookahead

There’s a solution provided in the “Get a Hint” section of this topic that goes like this:

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

My problem with the code mentioned above is that, why even use two Lookaheads when one does the job?

Please look at my code and let me know what is happening and why do I need to use two Lookaheads instead of just one.

let sampleWord = "astronaut";
let pwRegex = /(?=\w{4}\d\d)(?=)/;
//let pwRegex = /(?=\w{4}\d{2})(?=)/;
//let pwRegex = /(?=\w{4}\d\d+)(?=)/;
let result = pwRegex.test(sampleWord);

Do keep in mind that I passed this test with just one Lookahead. The second Lookahead is empty as you can see in the “pwRegex”.

Your regex passes all the test cases, but I think two lookaheads are necessary to do what the challenge wants.

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

Your solution would not match strings like ‘11astronaut’, for example.

Thank you for your response but I think I wasn’t clear in my post. My question is, why does it want us to use two Lookaheads when one does the job?

Since the challenge never mentioned to watch out for the strings like ‘11astronaut’, the first Lookahead in my regex can be modified to accept them.

Two Lookaheads only make sense if I had to watch out for those patterns as well.

Sorry if I am being an idiot or the whole thing is going over my head and I don’t even know but what were the people thinking when they designed this specific topic?
Like why make us use two lookaheads instead of just one knowing that we can pass the challenge with just one lookahead?

The challenge in the past asked for this

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.

With this one I don’t know how to use one lookahead only.

It seems that the challenge was simplified without considering that it can now be passed with only one lookahead.

Thank you for participating and for your response.

Were the conditions that I mentioned down below the same back then?

Your regex should use two positive lookaheads.
Your regex should not match the string astronaut.
Your regex should not match the string banan1.
Your regex should match the string bana12.
Your regex should match the string abc123.
Your regex should not match the string 12345.
Your regex should match the string 8pass99.
Your regex should not match the string 1a2bcde.
Your regex should match the string astr1on11aut.

Assuming the conditions mentioned above were the same in the older version of this challenge, the condition “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” conflicts with the 7th condition of the challenge which says “Your regex should match the string '8pass99'”.

I modified my regex to satisfy the conditions of the older version of the challenge. But it does not accept the 7th condition “Your regex should match the string '8pass99'” however.

let pwRegex = /(?=^[a-zA-Z]\w{1,}\d{2})/;

The conditions it accepts are:
Your regex should match the string bana12
Your regex should match the string abc123
Your regex should match the string astr1on11aut

The conditions it does not accept are:
Your regex should not match the string astronaut
Your regex should not match the string banan1
Your regex should not match the string 12345
Your regex should match the string 8pass99 (conflict)
Your regex should not match the string 1a2bcde (conflict)

If the 7th and the 8th conditions were removed from this topic since they conflict with the main rule/condition, which is “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”, my question still stands. Why use two Lookaheads when one does the job?

No, a couple where changed to agree with the looser requirements,

This was was should NOT match

You would need to load the previous version of the challenge, or creare the tests on your own

Why conflict here? This one doesn’t satisfy the conditions

Yes, that’s what I wanted to confirm actually.
Is there a way to load the previous version of this challenge?

I made my regex based on what you said and now that you mentioned that " Your regex should match the string '8pass99'" was actually " Your regex should NOT match the string '8pass99'", it means my regex is okay and there is no need for 2 lookaheads.

Is that right?

This one was a mistake.
The condition said “does not start with a number” but this one DOES start with a number. My regex does not accept this string though which is good.

uh… well, if it was the previous version I would make so to add a new test, but as it doesn’t… what about ab33? it should not match, but your regex matches

For previous version you need to know about version control and Git and GitHub, but it’s in the GitHub repo, freely accessible, if you know where to look - you would need to setup freeCodeCamp locally or use GitPod

1 Like

Oh, my bad.
Such a dummy.
I don’t know what I was thinking lol. :crazy_face:

I fixed my regex to satisfy the rule “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”.

let pwRegex = /(?=^[a-zA-Z]\w{3,}\d{2})/;

It does not accept your string ab33, it does not allow the strings that start with a number, it only accepts the strings that are greater than 5 characters long that end with two consecutive digits.

So, back to my question, why force people to use two lookaheads in this challenge when one does the job?

It probably is giving some guidance on how to solve it in the easiest way. In order to adjust for all the possible cases with just one lookahead, your regex is going to get quite long.
Yours still won’t match strings like ‘a12bcd’, though that password passes the criteria.

Thank you, jonathan.roley and ilenia, for all your help!