Regex lookaheads

I cant figure out why my code doesnt work for abc123

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

Challenge: Positive and Negative Lookahead

Link to the challenge:

Because positive lookaheads do not move the cursor, after both lookaheads your cursor is still at position 0, while your pattern says “from start and until the end of line”. Not moving from starting cursor position will not get you until to the end of line. You either need to move cursor or remove $ condition

i took out the $ and abc123 passed but 12abcde and astr1on11aut don’t, do you know why that is?

Because they both satisfy your pattern:

\D*    0 or more non-digit characters
\d{2,} followed by 2 digits

If you need account for all those cases you need to bring $ back and simply move cursor to the end of line by adding .+, so at the end you’ll have this rule: “Eat all characters, but make sure both lookaheads are satisfied”

Good luck!

I can’t pass 3 of the tests
Your regex should match “bana12”
Your regex should match “abc123”
Your regex should match “astr1on11aut”



**Your code so far**
        
```js

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

Challenge: Positive and Negative Lookahead

Link to the challenge:

Here you’re trying to eat characters after the end of line. I’m pretty sure you need to do that before

1 Like

yes i had to take that out, thank you so much!