One Error with Positive & Negative Lookahead

Tell us what’s happening:
Hey guys,

I have one error for the Positive & Negative Lookahead challenge, but I am not sure how to fix it. “8pass99” should not match but it does. I know it’s because matching passwords aren’t supposed to start with a number, but I am not sure how to fix my code.

Looking forward to some help on this one!

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36.

Challenge: Positive and Negative Lookahead

Link to the challenge:

Hello there.

Remember, \w matches any word character (equal to [a-zA-Z0-9_]). You are wanting something that does not match digits \d.

Hint: A non-character is achieved by using the uppercase selector for that character.

Hope this helps

Hey Sky,

Thanks for this. So, if I am reading this correctly, only the first positive lookahead is incorrect?

Not quite. Sorry, I only answered your question about not starting with a digit.

For the challenge, start with this:

  1. Does not start with a digit
  2. Has at least 2 consecutive digits
  3. Is longer than 5 characters.

Remember, not all of your regex has to be inside lookaheads.

Hope this helps

Awesome, thanks for explaining. I was able to pass the error above with the following code.

[^\d](?=\D*\d{2})(?=\w{6,})

However, I fail the following two tests now:

Your regex should match “bana12”
Your regex should match “abc123”

Thanks for being patient with me, on this one! Having significant difficulties with this one.

count the characters you match
one before the lookaheads then other 6 = seven characters

you need to remember how to match start and end of strings
maybe take a look at the list of challenges, it may sparkle something in your memory

I feel like I am getting closer with the answer below:

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

But, I am still getting the following error: "Your regex should match “astr1on11aut”. I believe it’s because the numbers aren’t at the end of the string and I’ve made my digits necessary for the end. However, I get the same error if I test the code below.

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

this says 0 or more non numbers followed by at least two numbers
but the “at least two consecutive numbers” doesn’t mean that can’t be other numbers in the string, instead you are imposing that those must be the first ones

1 Like

Well said! I was able to figure it out, thank you!

(?=\w{6,})(?=\D+\d{2})

Consecutive look arounds act at the same character position.

( Note that [a-zA-Z_] is the intersection of \w and \D )

This regex requires a minimum of 1 [a-zA-Z_] then 2 digits within
the first 5 word characters.

“8pass99” starts off with a digit, so a [a-zA-Z_] is not found before the 8.

However this “pass99” sub-string should match.

I think the task description needs some changes.

It is quite unclear about the expectations of the Regex.

1 Like

I am having trouble with the exercise, I found a solution which unfortunately is failing on one pass and that is th3 “8pass99” whereas the rest are passing without fail.

here is my solution:

[^\d](?=\w{5})(?=\w*\d\d)

I don’t know where the problem is…

you are probably missing anchors to indicate start/end of string, otherwise there is no way to say exactly how a string should start

could you please elaborate, thank you.
what do you mean by anchors?

review these two challenges about the anchor characters
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/match-beginning-string-patterns

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/match-ending-string-patterns

this did the trick for me /(?=\w{5}) (?=\D+\d{2})/

Thank you @ieahleen, they really helped .