Can anybody help me understand the solution? How will you express the solution in words if you have to?

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})/g; // Change this line
let result = pwRegex.test(sampleWord);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36

Challenge: Positive and Negative Lookahead

Link to the challenge:

Never Mind Just watched Beau’s tutorial . :laughing:

there should be at least 6 alphanumeric characters

and there should be two consecutive digits somewhere in the string - the \w* in front is to make sure that the digits can be anywhere, if it was just (?=\d{2}) they could have been only at the beginning

2 Likes

So correct me if I’m wrong here, everytime there is a condition to be met it means there must be another lookahead, am I right or wrong or how does one judge just how many lookouts there can/should be?

it depends on the pattern, if you have two different conditions that can’t be easily satisfied with the same pattern you could use a lookahead + normal patter or just lookaheads. Sometimes the conditions can be actually expressed with a single pattern. It depends on the situation.

Another question that has been puzzling me for a while now…why not use the wild card instead of the “w” in this example…or CAN I and the return will still be the same?

it depends on what kind of characters you are allowing, but considering that the other lookahead uses \w, you can use . in this one and there shouldn’t be much difference, but it can have different effects for strings that have the numbers more forward in the string

I don’t understand what you mean by the other lookahead, are you saying that if one . is used that . can’t be used again (. can only be used once)? (provided that . is used in the place of first \w)

no, I mean that . means any character, so it would not give any limits on what characters can be used, but \w instead matches only a limited set of characters
the other lookahead uses \w so there is already a limitation on which characters are allowed

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.