what is the need of adding \D* in this question answer Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, and have two consecutive digits.
let sampleWord = "bana12";
let pwRegex = /(?=\w{6})(?=\D*\d{2})/; // this line
let result = pwRegex.test(sampleWord);
i tried without adding \D* in the code but it didn't work, why?
Without the
\D*, the second condition would start the match from the position of the two digits, not from the first char.This will not match:
This will match:
That's because the second example satisfies both condition simultaneously (the second condition
(?=\d{2})without 4 other characters after the two-digits number cause the first condition(?=\w{6})to fail as12tesis not a valid 6-character long string. But, when we use12test, both conditions are evaluated correctly.The use of
\D*let the second condition consider also the characters before the two-digit number, automatically confirming also the first condition.