Positive and Negative Lookahead Doubt

Positive and Negative Lookahead Doubt
0

#1

Tell us what’s happening:
Why this code works?
i guessed it but i don’t have a reason why its working

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) 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


#2

first term:
(?=\w{5,}) this says ‘look ahead to see if there is a word of length 5 or more’
second term:
(?=\D*\d\d) this says 'look ahead to see if there is a series of non-numeric characters followed by 2 digits

putting these two terms in brackets makes them work together and requires both to be true for the test to return true.

try putting the whole regex into the www.regextester.com app if you want to play with each term and test against different strings for further understanding.


#3

It would have been easier to understand if they specified that the consecutive digits would always be at the end of the string as opposed to appearing anywhere in the string. Guess I should have observed that from the tests, but still.