Here\'s what I have, can someone please tell me where I am wrong?
Use .*
before \d{2}
since the consecutive digits may occur anywhere in the string. Your current regex should check for two digits to be present at very first.
let sampleWord = "bana12";
let pwRegex = /^(?=\w{5})(?=.*\d{2,})/; // Change this line
console.log( pwRegex.test(sampleWord))
Note that \w
matches only the word characters, so your regex fails if there input string contain 5 non-word characters. So for checking the string length, it's better to use .
instead of \w
.
let pwRegex = /^(?=.{5})(?=.*\d{2,})/;