I don't understand the solution: Positive and Negative Lookahead

  1. How does \d{2} ensure it is 2 consecutive numbers?
  2. Why does the solution say it’s (?=\w{5,}) but not (?=\w{6,})?
  3. Why does the solution say it’s (?=\D*) but not (?=\D+) when at least one non-number character has to appear?

Your code so far


let sampleWord = "bana12";
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_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36.

Link to the challenge:


?= stands for a positive lookahead,
\w for letters and underscore
{5, } minimal 5 maximal undefined quantity of letters
\D* stands for one non-digit
\d{2} for two consecutive digits