Regex password positive and negative lookaheads

I can’t understand why the answer to this challenge fulfills the requierments.

Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, do not begin with numbers, and have two consecutive digits.

  1. greater than 5 characters, digits or numbers, therefore six or more > (?=\w{6,})
  2. doesn’t begin with a number could be written multiple ways right?
    could a negative lookbehind be used here? (?<!\d) or ^[^\d] or (i found later)^\D
  3. has two consecutive numbers.
    this part is not location specific in the string >(?=\d\d)

let sampleWord = "astronaut";
let pwRegex = /^\D(?=\w{6,})(?=\d\d)/i;
let result = pwRegex.test(sampleWord);

Why doesn’t this work?
more specifically
why is the answer to strictly only allow 5 chars? why does the double digit portion not work? and why would the negative lookbehind not work as well?
There’s something about regex I’m just not grasping here.

I’m completely confounded by this challenge lol! I would appreciate any clarifications :smile:
Thank you!

when you have two more lookahead/lookbehinds near each other, they all look from same position

so writing this:

this will match only if second and third number of the string ate digits

1 Like

Okay that really clarifies the issue I was having, each lookaround needs to reference the whole portion of what your looking for, in other words they have to kind of agree with each other. I don’t know why that wasn’t clicking.

Thank you so much!