Excellent find!
I did solve the below with this: let pwRegex = /(?=\w{6,})(?=^\D+\d{2,})/;
But now I’m super curious. How would you solve it so it matches this: a0b11c
Excellent find!
I did solve the below with this: let pwRegex = /(?=\w{6,})(?=^\D+\d{2,})/;
But now I’m super curious. How would you solve it so it matches this: a0b11c
Let’s imagine we are the JS engine with the pattern in hands. We follow the pattern’s rules:
(?=.{6,})
(?=\D)
.+\d{2}
So the pattern is a combination of rules above: /^(?=.{6,})(?=\D).+\d{2}/
Wow! This clarifies several things for me including whether or not Lookaheads actually move forward or not. I think I understand them much better after your description.
Thank you very much for working that out for me (us).
Actually, I have to thank you . Only after reading your question did I reflect more about that Lookaheads feature. And I realized the key point of Lookaheads is not “moving forward”. By using it wisely, we can combine several rules into one. I think it is very useful in some cases.
Hello,
I am really struggling to understand regular expressions. On this problem specifically, I don’t understand how the solution correlates to the given restrictions:
The solution given seems to break all three of these restrictions.
The first part:
^(?=\w{6})
Doesn’t that mean “starts with 6 (and only 6) word characters” which includes numbers? Based on the written restrictions, shouldn’t passwords longer than 6 characters be allowed?
Second part:
(?=\D+\d{2})
Again, correct me if I’m wrong but doesn’t this mean “at least one non-digit followed by two and only two digits”? The restrictions never mentioned ‘only two’ consecutive digits, nor having to end with digits.
It doesn’t.
Your regex should not match “8pass99”
what i don’t understand is why u have inserted that ^ operator at beginning of final regex.
Because we have to “Stay at the begin of the password” first, then apply other rules.
It’s important to understand what this does.
Consecutive look arounds act at the same character position.
Therefore, the optional non-digit class \D overlaps with a word \w class.
This basically strips digits from the word class so that
the resulting class becomes [a-zA-Z_].
But it’s an optional class [a-zA-Z_]*
The intersection at a given character position is
any group of 5 consecutive word characters \w that has a
digit contained as one of the characters.
Therefore the \D is meaningless. It could just as well have
been a dot ‘.’ and it would be the same result. it’s because
the dot is restricted to be \w characters.
OK so thank you all for these comments, they are definitely helping me.
I thought ^ negates what follows? Is that only in brackets? and when it’s outside of brackets that means that it is saying to stay at the beginning like commented above?
Also, I’ve noticed in challenges they keep including the \D before the \d which doesn’t make sense to me. If we are just checking for digits why do we keep checking for non digits first??
I worked for me! This is my code
let pwRegex = /^\D(?=\w)(?=\w*\d{2})/
let sampleWord = “astronaut”;
let pwRegex = /(?=^\D)(?=\w*\d{2,})/; // Change this line
//first positive lookahead makes sure first char is not number
//second char makes sure there are two consecutive numbers after 0 or more alphanumeric char
let result = pwRegex.test(sampleWord);
Try this:
let pwRegex = /(?=^\D)(?=\w{5,})(?=\w*\d{2,})/
Too specific \w
pwRegex.test('$urpr15e!'); // false
I spend a lot of time for small mistake but I could not find any answer in here so I wanted to share mine.
For me correct answer:
let pwRegex = /^\D(?=\w{5,})(?=\w*\d{2,})/;
OR
let pwRegex = /(^(?!\d))(?=\w{5,})(?=\w*\d{2,})/;
OR
let pwRegex = /^\D(?=\w{5,})(?=\w*\d\d+)/;
There are 3 requirements in the question;
1 - greater than 5 characters long
2- do not begin with numbers
3- have two consecutive digits
Solution for each requirement respectively;
1- (?=\w{5,}) : This means the length of the string must greater than 5
2- (^(?!\d)) : the symbol ^ checks the beginning as you remember from step 16 or 17 title “Match Beginning String Patterns.” and ?! is negative lookahead because we do NOT want numbers(\d or [0-9]) at the beginning of the string. OR id you do not want to use negative lookahead you can use ^\D instead of (^(?!\d))
3- (?=\w*\d{2,}) : is for checking consecutive digits but it might be 1 digit or letters before them. So I prefer to use \w* especially for the case “astr1on11aut”. On the other hand, if you want you can change the part \d{2,} with \d+ both of them have the same meaning.