Tell us what’s happening:
Following is the example provided in the help text:
Here is a (naively) simple password checker that looks for between 3 and 6 characters and at least one number:
let password = "abc123";
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
checkPass.test(password); // Returns true
This is my interpretation of the regex, please let me know where/if I am wrong:
It’ll match “” if “” is immediately followed by an 3-6 alphanumeric characters which have a number after them.
And then this was the answer i came up with for the given problem. However, after reading the official answer, i think mine is incorrect but it passes all the test cases and other random passwords that I throw at it.
let sampleWord = "astronaut";
let pwRegex = /^\D(?=.*\d{2})(?=\w{5,})/; // Change this line
let result = pwRegex.test(sampleWord);
I think it should be wrong because this is my interpretation of the regex:
It’ll match a pattern that starts with a non-numeric character that has consecutive digits after it which are followed immediately by at least 5 alphanumeric characters.
So shouldn’t the regex return false for the string “Hi111i”, since the numbers aren’t followed by 5 alphanumeric characters?
However,It returns true. Why is that?
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0
.
Challenge: Positive and Negative Lookahead
Link to the challenge: