Positive and negative Lookahead challenge problem

Tell us what’s happening:

I was trying a lot of combinations, but whenever i change the lookaheads in the tests always keep exchanging the correct code for 8pass99 and 12abcde for bana12 and abc123, i don’t know what i’m doing wrong.
Can someone help me understand what’s wrong?

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0.

Challenge: Positive and Negative Lookahead

Link to the challenge:

Hi @laboon2077!

One of the requirements for this challenge was to create a regex that does not begin with numbers . So you need to add that symbol to your regex.

Also, there is a great tool you can use where you can test out your regex and it will provide you with detailed explanations of what is happening. Like a debugger.

1 Like

That is indeed an awesome site. Thanks for the tip.

1 Like

\D* at the start of the first regex doesn’t cover that?

You need to add the starts with symbol

i added ^, but still doesn’t work

Can I see your updated code?

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

Try moving this part out of the lookahead. Try putting it in front of the lookahead instead.

^\D

Also have you tried rewriting this part

{6,})

Like this

{5,}

You are really close.

I tried, but still doesn’t work

I tinkered with the code and when I rewrote this last part

// change this 
\D *
// to this 
\w*

it passes for me.

\w*

matches any word character (equal to [a-zA-Z0-9_])

Sorry for being so annoying
the code right now it’s like this:

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

And now it only trows one test error: Your regex should not match "8pass99"
all the other tests are okay

Sorry I should have been clearer.

This post here

Was referring to the end not the beginning.

Sorry for my poor communication. So if you left it the way it was where ^D\* was at the beginning then you only have to change this part \D+ to this \w*

Hope that makes sense.

1 Like

Thank you so much, i was stuck for hours

1 Like

Awesome! Congrats!

Regex is weird and was confusing for me too. :smile:
I would definitely bookmark that regex 101 site because you will need it for the telephone validator project at the end. It helped me out alot.

Happy coding!

1 Like