Regular Expressions: Positive and Negative Lookahead Questions

Tell us what’s happening:
My regex should match astr1on11aut, but I’m wondering why what I have doesn’t work and if there’s a small change I could make to it fix it without having the same solution that’s provided on FCC.

Your code so far


let sampleWord = "banan1";
let pwRegex = /(?=^\D)(?=\w{5,})(?=\D*\d{2})/; // Change this line
let result = pwRegex.test(sampleWord);


// pwd > 5, no beginning number, two consecutive

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0.

Challenge: Positive and Negative Lookahead

Link to the challenge:

your last lookahead, you are imposing “zero or more non digits followed by two digits” but you shouldn’t impose that the two digits are the first digits in the string

Do you actually mean the “last two digits in the string?” I didn’t see a caret to indicate the two digits were at the beginning of the string.

all lookaheads start looking from same position. One of your lookaheads has an anchor for start of string.

so the last lookahead is saying that the string should have zero or more non digits at the beginning of the string and then two digits

if you have some letters then one number then some letters then two numbers, the match fails

you can condense the first and last lookahead

I tried

 /(?=^\D*\d{2})(?=\w{5,})/

but then I get 3 fails. How exactly does the program view these statements? I understand this to mean "check at the beginning of the string for anything that’s not a digit or zero characters followed by two digits and check to see if the string is at least 5 characters

exactly, but that means that there can’t be other numbers before the 2 conseuctive numbers because you are using \D
so astr1on11aut will fail because you have non digits, a digit, non digits and then finally the two consecutive digits
instead you need to have astr1on11aut accepted by your regex

This regex will match that word
(?=^\D)(?=\w{5,})(?=\D*(?:\d\D+)?\d{2})

Broken down:

 (?= ^ \D )
 (?= \w{5,} )
 (?= \D* (?:\d \D+)? \d{2} )