Regular Expressions: Positive and Negative Lookahead - Help

So i’m on --> https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead

I reworked te example and got this answer:

let sampleWord = "99etu";
let pwRegex =/(?=\w{3,6})(?=\D\d{2,})/i; // Change this line
let result = pwRegex.test(sampleWord);
console.log(result)

I don’t get why the point of the \D? Is the pattern only going to work if the other one is present?

\D means no digit like A or J.

Check this thread for more detailed explination

Yeah but why is it in there in the first place? Why cant it just be the pattern of 2 or more numbers?

Well this /(?=\w{3,6})(?=\D\d{2,})/i is not the what challenge is looking for. It may passes the tests, but as you mentioned this \D looks redundant here, and of course it is.

Seems you just used one available default/sample pattern with some small changes that with luck it passed the tests.

This pattern you asked is wrong, check the thread link I shared the link it contains some detailed info about correct and expected pattern.

1 Like

no one mentioned the challenge. why are you?

I’m sensing English isn’t your first language, you can use your native language.

(I’ll just use Google translate)

Hi. I have the same question, and couldn’t figure out based on the answers you got here.
Why do I need the \D? Isn’t it redundant?

Thank you

I came here looking for the same answer, why the \D is needed.
I’m asking that because after repeated fails, i relented and went to the hint, where it gives the solution with \D. This however (i believe) is misleading, because while that answer will pass the tests, it doesn’t actually satisfy what it should.
After reading through a few times the thread mentioned above, i learnt that the (?=.d{2}) code only checks the START of the string for 2 digits, rather then searching the whole string (though i don’t understand why).
So, you need it to check a string where there could be any character before the 2 digits, and after the 2 digits (the 2 digits could be start middle or end). to search any character you use .* (dot for any character, asterix for 0 - infinite times). So, we place that before and after the 2 digits checker, and it’ll search for 2 digits start middle or end of the string.
let pwRegex = /(?=\w{6,})(?=.*\d{2}.*)/;
Now the /D works because the 2 examples it needs to pass, have letters before it, and no letters after it. Had a password been pass123pass, it would have failed, since it would have been looking for 2 digits at the END of the string.
So that’s the why, /D is used and passes, though it shouldn’t be. The better answer (explained also in the linked thread above) is more correct.
But if anyone can explain why (?=\d{2}) only checks the start of the string and not anywhere in it, i’d appreciate that. I feel it’s something basic that i’m missing. I’m going to assume for now the answer is simply, because that’s just how it works.

I tried to explain why \D* or .* is needed here. It MIGHT help, no guarnatees :slight_smile: . Also read the resources I listed there.