regExp - use of \D* before \d{2}?

Hej!

I’m struggling a bit to understand the regExp used in the lookahead challange when we need to match a password with greater than 5 digits and at least 2 numbers.

/(?=\w{6,})(?=\D*\d{2})/

I dont understand why we use the \D* before \d{2}.
as per my understanding we use the \D* to match anything that is not a digit, 0 or more times. Why is this needed before the \d{2} ?

let regex1 = /\w{6,}\D*\d{2}/;
let regex2 = /(?=\w{6,})(?=\D*\d{2})/;

I’ll try to explain this by comparing how regex1 and regex2 work differently.

regex1

  • regex1 doesn’t start with /^, so it could start matching at any string position (think zero-based indices).
  • Say regex1 started matching at string index 2 (i.e. 3rd character of the string), if successful with the \w{6,} part, it would have matched at least 6 alphanumeric characters (including underscore). At this point, the smallest index we could be at is 8, and regex1 would start look for zero or more non-digits followed by exactly two digits.
  • All in all, regex1 will match a string that’s at least 8 characters long. The string would have 6 alphanumeric characters appearing before 2 digits, and the two groups may be separated by zero or more alphanumeric characters followed by zero or more non-digits.

regex2

  • Similar to regex1, regex2 doesn’t start with /^, so it could start matching at any string index.
  • Again, say regex2 started matching at string index 2, if successful with the (?=\w{6,}) part, it would have matched at least 6 alphanumeric characters, although this time, we wouldn’t be at or after index 8. We’d be back at index 2 because assertions aren’t matched. Starting again at index 2, regex2 would proceed to check the (?=\D*\d{2}) part, which denotes 2 digits preceded by zero or more non-digits.
  • It may already become obvious to you that, if \D* was omitted, regex2 would be checking whether the character at precisely index 2 and index 3 were digits.
  • All in all, regex2 will match a string that’s at least 6 characters long. The string would have 6 alphanumeric characters, themselves including at least one of two consecutive digits or appearing before such digits.
1 Like

So regex2 could start matching at any string position as it does not start with /^.

But without the \D*, the second lookahead would only be checking at precisely index 2 instead?

Is this because it is the second lookahead? I understand why it works with the \D*, but I don’t see why it doesn’t work without.

yes

yes. in the example index 2 and 3, because we assumed regex2 started matching at index 2, as well as the {2} part.

Assertions assert but don’t match, and remember, once a regex starts checking at a certain index, it only moves on to the next character if the current character is matched.

1 Like

Ah this explains it very clearly to me.

Thank you so much for taking the time to explain in detail. I really appreciate it.

No worries. Regex can be really confusing when you first learn about it. When I was learning it a lot of my brain cells suffered while I was trying to wrap my head around some of the rules, lookahead/lookbehind being the worst of the bunch.

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