Positive and Negative Lookahead - further explanation

Tell us what’s happening:
Hey fellow code campers -

I have the correct code but I used a line from the example that I don’t quite understand. Can anyone explain:
(?=\D*\d\d)

Basically this look ahead looks for at least one number.
\D* - looks for not a number, any amount,.
\d\d - then looks for two numbers.

I may have answered my own question, so when writing these looks aheads and having double (?=) (?=) , the two lookahead statements are not added together, you have to search the entire string in both of them? Is that why we need the /D* in front?

Your code so far


let sampleWord = "astronaut";
let pwRegex = /change/; // Change this line
[spoiler]pwRegex = /(?=\w{5,})(?=\D*\d\d)/;[/spoiler]

let result = pwRegex.test(sampleWord);

Your browser information:

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

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

Please check following thread, includes explanation.

Happy coding

The important thing to take away about the lookahead is that it finds it but does not include it in a match

it’s necessary to be there for a match to occur, but isn’t included in a match

So you can use the positive lookahead to look ahead and see if there’s two consecutive digits, and then in the rest of the regex pattern check there’s 5 or more characters

Both have to hold for the match to succeed in finding it, but the value in the match will be the non-lookahead part

My wording may be a tad confusing, let me know if I can rephrase this

edit: the reason for the \D* in this case is because we’re not concerned with where the consecutive digits are, they just need to occur somewhere.

By padding the lookahead with any amount(including 0) of nondigits at the front it finds them anywhere in the string

2 Likes

Interesting. Ok so this is basically adjusting the search to look anywhere instead of only a specific place.

1 Like

Yes that’s right, else the lookahead would only check the current position being searched

1 Like

mySolution…
Does it contains atleast 5 alpha-numeric char or non numeric 3 times and atleast one numeric…

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

Please not your pattern would pass the test, but it’s wrong since it matches “bana1a” which shouldn’t. Or should match “JB007” which id doesn’t.

Please mind the two consecutive digits dear. Also mind the digit could be at middle too.

1 Like

Thanks for your reply.
Ill concentrate on the little things.

let pwRegex = /(?=\w{5,})(?=\D*\d{2})/;

Is these correct one.

One small note is the challenge needs the string should be greater than 5, so equals or greater than 6, so this is the first thing you may consider dear.

Also note about the second group (?=\D*\d{2}), it’s wrong becasue you supposed to have both \D*\d{2} together, so make them as a group, so you have (?=(\D*\d{2}))

Please note this pattern could match baba12s, but not the s(last char), since it’s out of the second lookahead.

Hint: as our second group expected any non digit at start, the same thing could be right about end of word.

1 Like

What baffled me for a few minutes is that these lookahead expressions are using the whole string all the time (well unless there’s a boundary set like ^ or $ or \b, etc…) so first I thought I need to check for the 5 char req. then the 2 digits, after each other, and it did not occur to me that both lookaheads need to be written to apply to the whole string and then the results of the 2 lookahead expressions are AND -ed together. Thus if both pass. then the regex passes, if one of the lookahead expressions fail then the whole regex fails. I hope it helps to some curious minds to understand it a bit more.

3 Likes

Not quite, it’s more like the regex engine looks right of its current position to check the lookaheads

That’s why you need the \D* - because we don’t care about the consecutive digits’ place being right there, it can occur later than the current position

1 Like

Ohhhhhhh…
Thank you for explaining that . I had the same question that was asked above.