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.
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.
This interpretation is incorrect. The regex is looking for a password that is between 3 and 6 alphanumeric characters with at least one number. This means a1b2c3 would pass, as would 22aa or any other combination.
Just because the (?=\w{3,6}) is before the (?=\D*\d) doesn’t mean the digits have to come after the letters.
This same logic applies to the regex you wrote to pass the challenge.
I think there is a typo in that page, let’s if I am able to fix it myself… (it is open source) I will at least open an issue for fixing it.
my experience and all other resources I have found say that what I said is correct
you can play with regex on a regex tester online (like regex101, just select on the left ECMAScript instead of Python)
it was probably an error
because if you write /(?=[a-z])(?=[0-9])/ it will never match anything as you can’t have a number and letter in the same place.
once I am at pc I will raise the issue, with explanation and stuff