I’m not very good at regexes either, but I’ll try to explain as best as I can from what I understand.
I think I get your question regarding the \D* in the second lookahead that if we’re already checking for \w in the first lookahead, then why we are checking it again in the second one (in a different way)?
That was quite confusing for me too.
The way I consider each lookahead is like a separate regex that must match the string. But in addition to that, ALL lookaheads in a regex must match the given string in combination with one another too, not just individually.
So from this perspective, we can explain the questioned regex as follows:
(?=\w{5,}) => Only match if the given string has at least 5 characters or more. (That
is fairly straight forward, no problem here)
(?=\D*\d{2}) => Only match if the given string has 0 or more non-digit characters AND the string also have 2 consecutive numbers.
So if we were to use only (?=\d{2}), this would translate into this:
Only match if the string has 2 consecutive digits.
That seems fine, it should work? But no, as I said earlier, it passes as an individual regex (if you think or test it separately) but it fails in junction with our first lookahead condition that says the string should have at least 5 characters.
So if we use this (?=\D*\d{2}), it would translate into:
Only match if the given string has Zero or more non-digit characters AND 2 consecutive digits. (By adding the condition of zero or more, we basically satisfy our first lookahead condition)
Now this passes as an individual regex and also in conjunction with our first lookahead.
Another variation which clears the fcc test can be /(?=.{5,})(?=.*\d{2})/.
The first lookahead is 5 or more ANY characters. And the second lookahead is Any zero or more characters AND 2 consecutive digits.
And lastly, yes, there might be a lot of different and weird ways to write this regex so that it passes all the given conditions.
Aɢʜʜ... I ᴅᴏɴ'ᴛ ʟɪᴋᴇ ʀᴇɢᴇxᴇs. I ʜᴏᴘᴇ ɪᴛ ᴍᴀᴅᴇ sᴏᴍᴇ sᴇɴsᴇ ᴛʜᴏᴜɢʜ.
(Edit: .*, \D* etc. in a lookahead (or not, I’m not sure) is something called backtracking. You may google it for more.
This might help a bit:
I am not able to post the link, getting 502 error.
Google this and see the stackoverflow answer:
Chaining multiple positive lookaheads in JavaScript regex
And also Google this: rexegg Regex Quantifier and see the first link, it might help too.
)