Positive and Negative Lookahead Regex- JS

I don’t know how to check if two numbers are consecutive here.
The current code gives an error and passes 12345 whereas it shouldn’t be passing it.
Can someone suggest how to check if two nearby numbers are consecutive in Regex?

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36.

Challenge: Positive and Negative Lookahead

Link to the challenge:

The question is saying greater than 5 characters, not 5 or more.

TLDR; The RegEx test() method and the freeCodeCamp tests (code case checker)
are two different things.

test() returns true if the Regex pattern matches a pattern in a given string and returns false if there isn’t a match.

The FCC code checker tests that the return value is what it is expected to be: true when it should be true and false when it should be false, which is how it determines if the code passes a particular set of cases or not.

Not sure what you mean by:

I tested your code…

let sampleWord = "12345";
let pwRegex =  /(?=\w{5,})(?=\D*\d{2})/;  // Your Code
let result = pwRegex.test(sampleWord);

The console output when clicking “Run The Tests” is:

// running tests
 Your regex should not match the string 12345
// tests completed

I DO NOT get the popup box below for your code…

pass

So actually, it doesn’t pass. If you are getting the popup window for the code you posted above, then I’m totally baffled and not exactly sure what you are doing.

I also went back and tested the “Official Solution” on the Hints page
and all of these input vs pattern cases pass the FCC code check for the Official FCC solution:

let result = pwRegex.test(1234567);  //returns true
let result = pwRegex.test(123456);   //returns true
let result = pwRegex.test(12345);    //returns false
let result = pwRegex.test(1234);     //returns false
let result = pwRegex.test(123);      //returns false
let result = pwRegex.test(12);       //returns false
let result = pwRegex.test(1);        //returns false

Why does FCC 's code check pass the ones that return false? …

The FCC code check determines whether or not the code returns a specific boolean value that it expects to be returned for several different cases.

The return value will only match what the code check expects if you have used a correct RegEx pattern.

Your code returns true for let sampleWord = "12345"; because case 12345 matches your pattern, but the code check expects the return value for case 12345 to be false.

Your pattern doesn’t satisfy the challenge requirement and so your code should not pass the code check for case 12345, and it doesn’t.

The FCC solution code returns false for let sampleWord = "12345"; because case 12345 doesn’t match the solution code’s pattern. The code check expects the return value for case 12345 to be false.

The solution pattern satisfies the challenge requirement and so it should pass the check for case 12345, and it does.

The return value (a boolean in this challenge) is letting us know whether the search pattern finds a match in the input string or not.

true means the pattern found a match, false means the pattern did not find a match.

Whether the return value is true or false doesn’t necessarily mean it should or shouldn’t pass the FCC code check. Rather, does it return true when it expects true and false when it expects false? If not, that means your RegEx pattern is incorrect.

So the FCC code check takes all of that into account.

TLDR; I breakdown your code piece by piece to help you see what it is doing, why it is not giving you the expected results, and why it isn’t passing all the FCC code check tests.

So, getting back to your actual code…

Doing a “Lookback” (lame pun intended :grinning_face_with_smiling_eyes: ) at the Lookahead challenge question, it says:

“Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, and have two consecutive digits.

(emphasis mine)

So the challenge wants you to match a pattern of more than 5 characters (which can be any character) followed by 2 consecutive digit characters (0-9).

As @twotani pointed out, do you want 5 or more (at least 5), or explicitly more than 5 characters followed by 2 digits? The difference between “or more” and “more than” is subtle but there is a difference.

If you don’t already have a good reference for the RegEx syntax, this is a good resource:
w3schools - JavaScript RegExp Reference

Let’s examine and break down what your pattern is saying…

The first part:

?=\w{5,}

is saying: Find 5 or more word characters (a-z,A-Z,0-9).

\w is word characters.
{5,} is a sequence of at least 5 (or more).

The second part:

?=\D*\d{2}

is saying: any string that contains zero or more occurrences of a non-digit character followed by 2 digit characters.

\D* is zero or more occurrences of non-digit characters (excludes 0-9).

It’s OK if the preceding characters are a-z, A-Z, 0-9 (\w* word characters).
\D* should work though, you are just being more specific here than it requires.

\d is a digit character (0-9).
{2} is a sequence of 2.

so a digit character followed by another digit character (2 consecutive digit characters).

So altogether, your code is saying:

Find at least 5 word characters
then find 2 consecutive digit characters
preceded by zero or more non-digit characters. 

Which is very close to, but not exactly what the lesson is asking you to do.

As I mentioned in my previous reply, if test() returns false then you know your RegEx pattern didn’t find a match, but if that is the expected result, it will pass the FCC code check.

try changing:

let sampleWord = "astronaut";

to :

let sampleWord = "12345";

and then add a line at the very bottom of all the code that says:

console.log(result); 

When you click “Run the Tests”, the console should output:

// running tests
Your regex should not match the string 12345
true
// tests completed
// console output
true

Which proves your RegEx matches on 12345 and returns true. If it was a working solution, it should not match on 12345and should return false.

I hope all that helps & makes sense.

Thank you so much.
This tiny logical error, took away almost an hour and a half for me.
It works perfectly,
when its

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

instead of {5,}
Forum kinda dope

We all share your pain. What we all need to be careful is not to start changing the working parts of the code out of frustration. The other day, I couldn’t figure out why the components are not appearing correctly and started messing with different parts of the code. Finally, realized that there’s nothing wrong with my js code. I was not closing the html tag correctly!?!

1 Like

Good point! I spent over 1hr once only to realize there was a : instead of a ; at the end of the line (due to my lazy shift key finger I guess) back a long time ago when I was writing a some PHP code in Windows notepad (no lines, no warnings to help to catch typos) Lessons learned! lol

1 Like

Why is the \D* necessary if we are only looking for two consecutive digits. if we left the second lookahead as (?=\d{2}) what causes this to fail

We aren’t just looking for 2 consecutive digits.

The \D* is saying zero or more occurrences of a non-digit character.

The challenge wants you to match a string of characters greater than 5 characters long (total length) and containing 2 consecutive digit characters (0-9).

So you aren’t just trying to match 2 numbers next to each other in the string.

abcde11 should match, 12345 shouldn’t match.

I don’t think the \D* is necessary depending on the solution approach (there is more than one way to solve this). I was just breaking down what the OP’s solution was literally searching for.

But I think (?=\d{2}) by itself is just looking for 2 consecutive digits, and not checking if they are preceded by a specific number of any other characters.

So it would match “a12”, “ab34” (strings that are not greater than 5 characters) which is shorter than the requirements in the challenge and thus it fails the tests.

1 Like