Regex Unexplained code

I am going through some regex code. The Challenge is called, ‘Positive and Negative Lookahead’ Below you will find where I have found to be confusing.

** A more practical use of lookaheads is to check two or more patterns in one string. —""“Here is a (naively) simple password checker that looks for between 3 and 6 characters and at least one number:”""

let password = "abc123";
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
checkPass.test(password);
``` **

In the triple quoted statement above, it clearly states that the password checker would look for 3-6 characters and at least 1 number (\d). 

My question is, How is it so? Clearly only one character is permitted since there isn't (+ or *) infront of (\d). Also, what is the purpose behind (\D*)? Since (\D) is shorthand for ([^0-9]).

the regex hinges on the lookaheads, which are non-consuming, unlike normal regex that looks one character at a time, never goes backwards.
Lookaheads tries to fulfill the regex set out in the brackets by looking forward into the string, and if that regex is true, goes back to where the lookahead started.

In the example regex given by FCC:
first lookahead: (?=\w{3,6}) look to make sure the string is only alphanumeric, and between 3 to 6 characters, if that is true, return to where it started (index 0 in this case) and move on to next thing in the regex
second lookahead: (?=\D*\d) looks to make sure there is at least one number in the string, the \D* is necessary to make sure even numbers later in the string can be found, or else it will only accept strings that start with a number as true. If this lookahead is true, return to where it started (index 0 again) and move on to next thing in the regex

since at this point there are nothing left in the regex, the string passes the regex.

playing around with regex on sites like regex101.com can help understand them better

First and foremost, I appreciate the help. Secondly, I understood the first bracketed regex expression. I am having trouble understanding how \D* is being applied. What I require is a simple explanation of the concepts occurring in the second bracket.

  1. For example, why is the \d allowing more than 1 number without including the + or * or {n} signs?

  2. Does \D* have a different meaning to \D? If so, what is the meaning, I have not had an fortune locating this question online yet.

because lookaheads are non-consuming, you don’t need to account for multiple numbers consecutively, or multiple instances of numbers spread out in a string, you only need to find a number within the string. Let’s lay out a few possible scenarios and see how the lookahead works out.

A refresher on * in regex terms, it means match previous term 0 or more times

const str1 = "ab1";
// walking through (?=\D*\d), we first go through "ab" and sees that they fit \D*, then we reach 1 which fits \d. The lookahead returns true

const str2 = "ab12";
// we first go through "ab" which is \D*, then 1 which is \d. we exit right here as all of the lookahead is satisfied, the 2 is never reached and returns true

const str3 = "ab1ab";
// same as above, we only get to 1, "ab" is never looked at, returns true

const str4 = "ab12ab";
// same as above, "2ab" never reached, returns true

const str5 = "1ab";
// the lookahead finds no \D which is ok, since we can match that 0 times, then we match 1 with \d, exit and return true, "ab" is again never looked at

const str6 = "12ab";
// same as above, "2ab" is never looked at

hope that clears it up a bit

1 Like

I see. Thank you for your assistance. All the best.

1 Like

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