Positive and negative lookahead challenge

Tell us what’s happening:
why the need for the two positive lookahead. i need explanations on what each lookahead is doing.
also, whats the need of non digit (D+) before /d
also, ^\w is alphanumeric. which means alphabet or a number can be at the front of the string. but the challenge said do not start with number.

Your code so far

let sampleWord = "ban56fadrrr57";
let pwRegex = /^(?=\w{5,})(?=\D+\d{2,})/; // Change this line
let result = pwRegex.test(sampleWord);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36.

Challenge: Positive and Negative Lookahead

Link to the challenge:

Since the regex starts with ^ which means it checks both of the two lookahead for first character of the string.

first lookahead (?=\w{5,}) means at least 5 alphanumeric character long string and alphanumeric characters are a-z,A-Z,0-9 and _ .

second lookhead (?=\D+\d{2,}) where \D+ means at least one non digit character and \d{2,} means at least two digits. second lookahead match any string start with a non digit character follwed by at least two digits .

Since string should match both lookahead and the string starts with a number matchs the first lookhead but not the second so any string starts with number will return false.

1 Like

Thank you so much. Now i have a clear understanding of how it works