Positive and Negative Lookahead related problem

Tell us what’s happening:
The task is to use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, and have two consecutive digits. I use two lookaheads in the code, and tried that they two worked fine seperately with the test sampleWord “8Pass99”.(Means when in the code when I only use (?=\d{2,}) or (?=\w{6,}), the output is true, but when I put these two together like the code below, it returns false.)

Your code so far

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36

Challenge: Positive and Negative Lookahead

Link to the challenge:

Hey there.
There is a detail regarding lookaheads and lookbehinds that is easy to miss/forget. And it is that all the lookaheads and lookbehinds must pass starting from a certain position(like an index of sorts). Let’s take one of the tests given in the course, say 'bana12'. So. this lookahead /(?=\d{2,})/ starts checking the first character 'b', which is not a digit, so it moves on to the second one 'a', which is not a digit again, and so on until it reaches '1'(which is the 5th character in the string). Then it finds '2' at the 6th position and it passes. Now, the other lookahead /(?=\w{6,})/ will succeed right away, because it sees 6 characters ahead right from the start, but it won’t succeed from the 5th position(where the first lookahead succeeded), because starting from there, it will only see 2 characters ahead. A good analogy is this regex /^\d{2,}/. It will match 12, 125, 44444444, 99aba but it won’t match "bana12". You must modify this /(?=\d{2,})/ so that it passes right from the first position as well.
Hope this is helpful.

Thank you sooooo much, I get it perfectly now. I did change it to /(?=\D*\d{2,})/ to get pass the task, but was still obssessed with why the original one was not working. Your answer definitely made my day!!

I had difficulty initially but by adding \ D (so no number) followed by \ d I was able to pass the test. my only problem was that i could not relate password 123456
Lascio di seguito il codice con un breve ragionamento

  1. Create an alphanumeric password so with \w I could do it
    2.Set it to have two consecutive digits so \d{2,}
  2. Enclose everything in a minimum length of 5 characters {5,}

I hope this will help make your exercise easier, try to solve it without spoilers

I have removed the solution from the post. It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

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