Regular Expressions - Positive and Negative Lookahead

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

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

Isn't \w{6} saying match passwords that are exactly 6 characters long? Why wouldn't greater than 6 characters long be \w{6,} ?

Also, to satisfy the 2 consecutive digits, why is it not just(?=\d{2})? What is this w* stating (?=\w*\d{2})?
The way I read the second half is there can be 0 or more characters with 2 consecutive digits. Doesn't make much sense to me with the w*. Any insight would be greatly appreciated.

Your browser information:

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

Challenge: Regular Expressions - Positive and Negative Lookahead

Link to the challenge:

Looks like what will help you is to read this

I have. It says x {n } matches exactly “n” occurrences not a range like greater than which we were looking for in this problem.

1 Like

Yes but if you look at where you read that and then scroll up about three rows in the table, you will find the answer to your question

That is referring to the second part of my question. What i just wrote prior was referring to the first part of my question. I did read that which is why I’m confused on how it applies to this problem.

Matches the preceding item “x” 0 or more times. in MDN

I wrote in my question, “The way I read the second half is there can be 0 or more characters.” \w*

Just trying to figure out why that needs to be added instead of (?=\d{2})

I wrote

The question description is

Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, and have two consecutive digits.

I do not see anywhere that they make you do it one way or another.

Your challenge is to find a pattern that will perform the match. (You are not forced to use a specific pattern).

Understood. I just feel like I’m missing something but hopefully it gets cleared up along the way. I appreciate your feedback!!

1 Like

yes, but regex always match the part of the string that has the pattern. To have a string that has exactly six characters, you need to use anchors to match the beginning and end of the string.

the two lookaheads match the same position and look at the immediately following characters, if you use only this, you are saying that the first two characters matched by the other lookahead must be numbers, instead the numbers can be anywhere

1 Like

Very much appreciated!