Trouble with Positive and Negative Lookahead regex challenge

Challenge: Positive and Negative Lookahead

Link to the challenge:

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

that regex don’t pass in 2 requirements, Your regex should use two positive lookaheads. and Your regex should not match the string 12345.
I changed the regex:

let pwRegex = /(?=.{6,})(?=\d{2,})/; // Change this line

Your regex should match the string bana12
Your regex should match the string abc123
Your regex should match the string 8pass99
Your regex should match the string astr1on11aut

The corret regex is /(?=.{6,})(?=/D*/d{2,})/, but i don’t underset why the lack of /D* broke my regex, and i don’t want just pass the tast without knowledge.

I’m improving my english, please be patient.

Hi, your English is fine, don’t worry.

The issue is on how normal regex consumes characters while look aheads do not.

A normal regex goes through a string, from index 0, 1, … and so on.

look aheads checks for something happening after a regex match.

Let’s look at bana12

let’s say you start with something like /(?=\d{2,})/, you are saying match the character ‘’ (empty string) if it is followed by 2 or more consecutive numbers. So it finds a match at the empty string between ‘a’ and ‘1’.

now we add the other lookahead and make this /(?=\d{2,})(?=.{6,})/ you have added on a condition to that empty string you are trying to match. Now in addition to the empty string must be followed by 2 or more consecutive numbers, it must also be followed by more than 5 characters.
The empty string between ‘a’ and ‘1’ only has 2 more characters following it. There is no match. And that’s it, the look ahead doesn’t look ahead any further.

Now think about adding \D*, the look ahead /(?=\D*\d{2,})/ matches every empty string that has any number of non-numbers and then 2 or more numbers following it. Now there’s a match in the empty strings:

where the | is the matched empty string.
Now we add on the second look ahead and realize that the first empty string before ‘b’ also has more than 5 characters following it. That is a match.

Let me know if that makes it clearer a bit, regex is a bit of a pain to explain through text. Please ask if you need more clarification

Hi there buddy,

I hope the following notes will help you solve the exercise:

  1. first, we use ‘regex’ to create a pattern that will catch multiple possibilities of correct string patterns (example: passwords);

  2. when you create a regex, always think that it should be flexible to handle any pattern of strings that fits the requirement/s; for this exercise, the requirements are ‘pwRegex’ should match passwords:
    a) that are greater than 5 (alphanumeric) characters long — this means at least six of any combination of alphanumeric characters (which, includes the requirement in b),
    b) and have two consecutive digits

  3. take for example, the cases of the following: (to give you some ‘visual aid’)
    a) “8pass99” : “8 - pass - 99” ----- you have: (‘beginning’ - ‘middle’ - ‘two digits’)
    b) “astr1on11aut”: “a - str1on - 11 - aut” ----- you have: (‘beginning’ - ‘middle’ -‘two digits’) - (‘end’)

  4. on number 3)a, the string starts with a number 8 followed by alphabetic characters, then followed by two digits —
    your partial ‘regex’ may look like this: /^[0-9]\w{3,}\d{2}/ ---- which, means the 'beginning 'of a string is a digit , then ‘followed’ by at least 3 occurrences of any combination of alphanumeric characters (‘greedy match’ like a ‘pacman’); then, once ‘regex’ sees occurrence of two consecutive digits, then it thinks there is a valid match;
    on number 3)b, the string ‘begins’ with an alphabet character ‘followed’ by alphanumeric characters, then ‘followed’ by two consecutive digits, and then ‘ends’ with alphabetic characters — (so, the only thing that is different in 3)b is that it ‘ends’ with alphabetic characters
    your partial ‘regex’ may look like this: /^[A-Za-z]\w{3,}\d{2}\w*$/

  5. for the purpose of this exercise, our goal is to group together the right combination of patterns in a ‘positive look-ahead’ ; on this exercise, we are required to come up with two ‘positive look-ahead’ groups; based on numbers 3) and 4) above, you should be able to figure out the rest and come up with something like this: /(?=)(?=)/

Hope you found this helpful to understand the logic and usefulness of ‘regex’ to solve problems like this one.

Happy coding!