Interpretation of Positive Lookahead

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:

lookaheads are zero width, so they both look from the same position

I think it is the only thing you were missing in understanding how they work
otherwise ask again

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.

Thank you for answering.

What does this mean? Also, could you please share a source for this.

According to this article:

More complex tests are possible, e.g. X(?=Y)(?=Z) means:

  1. Find X .
  2. Check if Y is immediately after X (skip if isn’t).
  3. Check if Z is immediately after Y (skip if isn’t).
  4. If both tests passed, then it’s the match.

In other words, such pattern means that we’re looking for X followed by Y and Z at the same time.

Doesn’t this mean that don’t look from the same position and Z follows Y?

Thank you for answering.

Please refer to my previous comment.

Doesn’t this suggest that the order of Y and Z matter?

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)

this one seems pretty complete:

what you’re saying would explain why my regex is working.


Take a look at page’s change log:

They changed what you are saying to the current text.

humans get things wrong

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

You’re right. It does not. Thank you!

Out of curiosity, how do you do that?

you found their github repository
just create an issue there linking the page and explaining what would need to be changed, and why

you can do it yourself for experience

if you feel up to it you could even make the change yourself

I have created this issue in their repository:

1 Like