Regular Expressions - Positive and Negative Lookahead

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

Why do we have to add the none-digit \D* first and add additionally \d?
It can’t be done just \d to indicate at least one number?

Your code so far

let sampleWord = "astronaut";
let pwRegex = /(?=\w{5,})(?=\D*\d{2,0})/; // 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/ Safari/537.36

Challenge: Regular Expressions - Positive and Negative Lookahead

Link to the challenge:

if at least 2 consecutive digits are required, i was thinking of using \d\d+ or \d{2,}. I don’t understand the usage of \D in this context as it means none-digit

Did someone tell you that you have to use \D in your regular expression? You don’t have to use that specifically, but you do need to make sure you can catch double digits appearing anywhere in the string.

Much appreciate your explanation!

But what if we take first lookahead from this discussion (?=\w{5,}) and change sampleWord = “astronaut” to smth like “…astronaut” (instead of dots we can use any characters that are not included in /w character class)
e.g. :

let sampleWord = "..........astronaut";
let pwRegex = /(?=\w{5,})/; 
let result = pwRegex.test(sampleWord);

So the value of ‘result’ variable will be true, though \w character class includes only [A-Za-z0-9_]
In context of explanation above, how could this be possible?
I really got stuck, because explanation is clear and correct, but contradictory at the same time

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