Positive and Negative Lookahead - \D*

Tell us what’s happening:

Hi guys, this code works but i don’t know the use of “\D*”…without it my code failed !

If \d+ means 2 consecutive digits,why would i need \D ??

Your code so far


let sampleWord = "astronaut";
let pwRegex = /(?=\w{6,})(?=\D*\d+)/; // Change this line
let result = pwRegex.test(sampleWord);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead

other people have asked this question before on the forum so you can easily find the answer if you search

for eg. if you read the thread comments in this post you can get more information

if that’s not enough there is a lot more similar asked and answered questions on the forum.

Your regex should match “abc123”…The challenge wants abc123 to be matched and it has 3 consecutive digits !..i searched through other forms but i couldn’t really understand. some one chose to use the .* which would have the same use (not function just use) as \D* in my code but i still don’t get it…if the lookahead scans the entire string looking for the thing i identified ,why can’t \d+ see all the consecutive numbers ? why do i need to put something before it or after it if the whole string is scanned ?
Example:
let sampleWord = “abc123”;
let pwRegex = /(?=\d+)(?=\w{3,6})/;
console.log(pwRegex.test(sampleWord));
this code returns true because the lookahead searches the entire string and the actual place of the regex does not matter in the lookahead.

looking ahead for 2 consecutive digits (\d+) only will also mean matching 3 and 4 and 5 etc. consecutive digits, when what they want is not to match ‘123’ and not to match ‘1234’
So that’s why you need the non-digit \D added to the pattern so you don’t match those numbers.

i thank you for your patience with me but it did really not match them…so what’s the problem ?

what I was saying that was \d+ matches 2 or more. (I wasn’t referring to the whole sentence and I was responding to your last post where you were expressing surprise that the test wants to match a phrase with 3 digits).
the first part of your regex (the first group) looks for the correct length
the second part should look for the correct ‘formatting’ in a way.

(the first part says I need to match a certain length, and the second part says I need to match certain type which has non-numerical characters plus numerical characters. The numerical characters should be 2 consecutive characters)

The above screen shot (from www.regestester.com) shows that the second term in your regex will not match ‘abc’ because obviously it only looks for numbers. Try to look at the whole exercise and see the whole picture and match up what you wrote to that.

3 Likes

I wish to thank you soooooooo much.That quote + several examples i tried on the VS clarified everything for me.Can’t really express my gratitude towards u.God Bless u.

1 Like

The way I read the second look-ahead is:
All non digits (\D*) followed by two consecutive digits or more…
I got confused too…