Help with RegExps! Positive and Negative Lookaheads

Hey all,

The regular expression section is kicking my butt. I got hung up on the “Restrict Possible Usernames” lesson for hours, have skipped it for now, and now I’m hung up on the positive and negative lookahead lesson. My code:

let sampleWord = “astronaut”;

let pwRegex = /(?=\w{5,})(?!^\d)(?=\d\d)/;

let result = pwRegex.test(sampleWord);

… should return true when sampleWord is greater than 5 chars, doesn’t begin with a digit, and has 2 consecutive digits. But when I run the tests, ‘bana12’ and ‘abc123’ both fail. What’s wrong with my pwRegex?

all the lookaheads start looking at the same position. They need not to be mutually exclusive.

Thanks for the response.

The lesson specifies it needs two positive lookaheads, when I try grouping them together, I fail another test. Also, not sure how to make them inclusive ==>
is this:
/(?=\w{5,})(?=\d\d)/
the same as:
/(?=\w{5,}\d\d)/
… do I add a comma in there or something? extra parens?
… and do either of those expressions above look for words 5 chars or more long, with 2 consecutive digits?

they don’t do the same thing

this will match if there are five consecutive word characters, of which the first two are numbers

this match if there are at least five word characters followed by two digits

both, but they are really rigid in the way they do it. neither will let pass all the allowed passwords, and maybe will even let pass some of the not allowed ones

Some facts to help you.
Assertions exist between characters not at character positions.
This lets them look ahead or behind.
Its just like a camera takes a picture of an object, where the camera is not the object.

Consecutive assertions operate at the same between character position.
If they can’t be satisfied at one spot, they move to the next spot until they are
all satisfied.

Digits are word characters, so your regex there is looking for 2 digits, then 3
or more word characters right after it.
“(?! ^ \d )” specifies that the first digit cannot be at the start of the line.

Usually this has nothing to do with Names.
And, is better written as
" (?!^)\d\d\w{3,}"
because there is no valid reason for the assertions at all, except (?!^)

Who wrote this regex, it’s not one with too much meaning to it.