Pos/Neg Lookahead Challenge Question

Continuing the discussion from freeCodeCamp Challenge Guide: Positive and Negative Lookahead:

This was my solution for the regex challenge:

/(^\D)(?=\w{5,})(?=\D*\d{2})/

But my question is it didn’t work without the D*, why? Specifically it didn’t match bana12 or abc123

5 Likes

Hi there…
It looks like you are making good progress on this. You are really close.
Remember, it is ok to step back, shake that head out and come back into the logic again, LOL.

You have 2 Pos Lookahead groups set up. Both need a little tweaking still.
First thing:
You don’t need the capture group in the very beginning. Hmm, where else could it’s purpose go… and what would the result be.

Second thing:
The 2nd Lookahead group could use a little tweaking somewhere.
** Remember that Lookahead groups require you to think from the beginning of the string again. So how could you adjust it to look from the beginning. Without copying the whole 1st Lookahead group, how else could you account for all the characters after the beginning flag up until you get to the last set of digits to deal with.
Also, consider just how many digits you are trying to deal with for the end of string.
(3 good hints, LOL) Good luck!

I find it challenging a lot when it seems like what I’m doing is correct and should work, but I just can’t find enough supporting examples or documentation to support the problem at hand, or if there is possibly more then one solution to the problem, but the exercise is looking for a specific solution.

8 Likes

you have a capture group that day to start the string with a single non number character, after that you have two looks heads, they start looking from the same point, so if you write one of the two saying that it needs to find two numbers, than your string will pass the test only if it has at the beginning exactly one non number followed by two number characters

3 Likes

You regex exercises are too complex and you don’t bring enough clear explanations. I will have to look for help on another platform, less abstract than you.

12 Likes

awesome! google it till you get it!
you are putting in practice the Read-Search-Ask Technique suggested by FreeCodeCamp

9 Likes

This is a criticism, not an application of doubtful FreeCodeCamp advices.

2 Likes

you have never asked a question in the forum so I don’t understand your criticism to people that answer questions

2 Likes

Can someone explain which part of the regex covers the condition to not include a number in the beginning? \w - matches letters and numbers, right?

the second lookahead starts with \D which is the same as [^0-9]
it means that it can’t start with a number
remember that lookaheads are zero-width things, so they both check from same position

9 Likes

I looked at the answer and I don’t understand how ^(?=\w{6}) can be legal for the part of the question where it says password must not start with a number. Doesn’t \w also contain the values (0-9 and _)? Also the question says nothing about any special characters needed to be involved in the solve but the solution obviously specifies one non-word character plus two digits. I feel like the wording should be changed for the question. Please let me know if I’m wrong, I’m learning here like everyone else.

4 Likes

you are right that \w includes numbers, but the other look ahead start looking from the same position, and that one has a \D at the beginning, so there can’t be a number at the beginning of the string

1 Like

So the ^ carrot applies to the second paren() group for the first character? because the way I took the look up was that ^ carrot means must start with for the first parenthesis group. Because I’m just trying to learn would the carrot have applied only to the first parenthesis group if I had put it in a character case like
^[?=\w{6}]

I would have thought the answer would have been something more along the lines of
^[a-zA-Z]{6}(?=\D+\d{2,})

But what you’re saying is that forward lookups no matter what parens group they start in get evaluated from the start of the string.

I was also admittedly confusing \D with \W for special character. Some of these challengers were pretty ruff for a beginner. Do programmers use regex a lot? I struggled with recursion stuff a lot also but my programmer friends said while its important to understand the concept they rarely use it in everyday work.

lookaheads have a width of zero, so they match a position, not characters
put one after the other two things of zero width, the total width is still zero, so they match the same position, and the characters after that position needs to satisfy the pattern of both lookaheads
they start at the beginning of the string only because of the ^, they can be used together with all the other stuff you learned so far.

how much regex is used depends on what you do, and most often than now you can google and find the regex ready to use online, and at that point no one will say anything about that
you need a password validator? google for it, you will find it ready.

this stuff is to learn a bit of how regular expressions work, so if you will find yourself working with them you will not be lost
actual mastery will need a lot of time, and actually needing working on them

6 Likes

Thanks I think you really helped me clarify that concept in my head.

hey, if anyone’s still finding the lookahead concept in this challenge confusing, here’s an article that explains the concept in very simple and succinct terms… FCC i think the ambiguous wording is the problem. AND the explanations aren’t enough before thrusting someone into the deep end. esp in a complicated subject like this.

javascript.info/regexp-lookahead-lookbehind

21 Likes

Thanks for sharing that. It actually helped a lot

2 Likes

I’ll give it a glean too. Cheers

2 Likes

Hi everyone,

/(?!^\d)(?=\w{5,})(?=\w*\d{2})/
I am getting a wrong answer for the above regex because ’ Your regex should not match “8pass99”. ’

If i tried /(?=^\D)(?=\w{5,})(?=\w*\d{2})/ then it passes all tests.

  1. Can somebody enlighten me why did the first one fail? Doesn’t (?!^\d) mean 'continue matching if string is not the pattern ^\d?

  2. Isn’t (?!^\d) equivalent to (?=^\D) ?

2 Likes

Hello. Just checking something.

FCC solution:

let sampleWord = “aaa123”;
var pwRegex = /^\D(?=\w{5})(?=\w*\d{2})/;
let result = pwRegex.test(sampleWord);

Does (?=\w{5}) mean that the password is limited 5 characters? Or is it simply checking that the password has 5 characters? In my solution I wrote {5,} instead of {5} so that it would be 5 or greater.

Actually, the challenge asked us to have a password “greater than 5 characters long”, so in both cases shouldn’t it be at least {6,}?

Also, the positioning of ^D seemed to change the outcome too. When I tested the FCC solution, a password of aaa12 was false. However, my solution was:

/(?=\w{5})(?=^\D\w*\d{2})/

So that ^\D was in the second look-ahead. This meant that a password with 5 characters (aaa12) was true., which wouldn’t be what the challenge was asking for. It still passed but that’s because it wasn’t included in the test criteria.

Sorry if this is all trivial or poorly explained, but if anyone could clear this up for me it’d be greatly appreciated.

3 Likes

I dont understand the regex of the example:
-According to documentation the meaning of the 1º lookahead is
(?=\w{3,6})
The string has to contain at least 3 characters and a maximum of 6 characters. Then why this string “abcdef1” with 7 characters passed the checking?
Could anyone explain me what is happening here?