I’m really just trying to understand the second lookahead in this expression. Can anyone explain:

``````/(?=\w{3,6})(?=\D*\d)/;
``````

From what I understand, it’s looking ahead for zero or more absence of digits (\D*) and also a single digit(\d). This doesn’t compute in my head.

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

Challenge: Regular Expressions - Positive and Negative Lookahead

The exercise describes it as “and at least one number”.
So in addition to the first lookahead wanting 3-6 characters, the second one makes sure that there is at least one number in there.

1 Like

I understand what it accomplishes, I don’t understand why this expression makes sure that there is at least one number present.

\d represents [0-9]
\D represents [^0-9]

So \D* returns 0 or more of everything except a digit and \d returns a digit.

Why is \D* needed? why not just \d?

While typing this I start to understand the expression. (?=\D*\d) is asking for 0 or more of any characters AND 1 digit. So to put it all together, `/(?=\w{3,6})(?=\D*\d)/` is an expression that looks for:

1. A string with at least 3 characters, and at most 6 characters
2. A string that has 1 digit

I rest.

you got it!
(I love how you explained it all to yourself!)

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