Regular Expressions - Positive and Negative Lookahead

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

Hi, I have a fast theory question here. In the correct answer to this question, we are supposed to specify in a regional expression that passwords have to be greater than 5 characters long, whatever those characters are.

I had chosen to use \w{5,} to ‘at least’ make sure in the lookahead that the password would hit at least 5 characters. I’m fine with all other aspects of the code, but I do not understand why we are working with {6} here.

As I recall from the ‘Specify Exact Number of Matches’ lesson, using the curly braces indicates that we are looking for that exact match - in this case, exactly 6 elements/characters from the \w category.

I’m uncertain why the \w{5,} wouldn’t work, as that would hit at least 5 characters from the \w shorthand, and it wouldn’t require a 6 character only password.

Thank you all for the help!

Your code so far

let sampleWord = "astronaut";
let pwRegex = /(?=\w{5,})(?=\w*\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_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Regular Expressions - Positive and Negative Lookahead

Link to the challenge:

Hi, using \w{5,} your password is at least 5 characters in length which means your regex accepts a password with 5 characters.
Here, we are expected to give a password regex with greater than 5 characters so we could use \w{6,} (at least 6 characters) or \w{6} (exactly 6 characters)

1 Like

Ah okay, I didn’t know if we could use {6,} or {6}. It made it look like it could only be 6, but that makes much more sense. Thank you!!

I’m glad I could help!