FCC Regex help?

Still can’t wrap my head around this, even after using anchors, not using anchors and using different techniques. I know what is happening and mostly how it works but idk


let sampleWord = "astronaut";
let pwRegex = /(?=\w{5,})(?=\D*\d{2,})/;
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/84.0.4147.89 Safari/537.36.

Challenge: Positive and Negative Lookahead

Link to the challenge:

Hint

starts with a number is ^\d

It doesn’t work :frowning:
I tried it on both cases, it only needs it on the pattern matching one though I assume. Can you try to use it on your browser with my code and see if it works?

I tried the caret^ only, then the $ only and then both.

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

Note: \D is used to find a non-digit character
Hint: ^\d is used to find a digit from 0-9 at the beginning.

(?=\w{5,}) is looking for 5 or more word characters.
(?=\D*\d{2,}) is looking for zero or more non digit characters followed by 2 or more digits.

But we aren’t looking for digits at the beginning right? So

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

this fails with

Your regex should not match "12abcde"
Your regex should match "astr1on11aut"

Correct digits should not be at the beginning. Your code is written incorrectly. You need to check for two consecutive digits.

\D* matches any string that contains zero or more occurrences of D .
{2,} two or more times
{5,} five or more times
{2} exactly two times
\w word character

2 Likes

I passed with this:

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

I’m still trying to fully grasp how this works,
so : At the beginning there was God created the heaven and earth…
nah jk

At the beginning check for 2 or more non-numbers,followed by as many (zero or more) alphanumeric characters and atleast 2 numbers. But then I thought that I can check for just one or more as well so this works too:

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

I guess I forgot to add what the regex should check in-between the \D+ and \d{2}…
Thank you for the help!

Alright so I’m failing two tests with this code:

Your regex should not match "8pass99"
Your regex should not match "12abcde"
let sampleWord = "astronaut";
let pwRegex = /(?=\w{5,})(?=\D*\d{2,})/;
let result = pwRegex.test(sampleWord);

I have tried changing the non-digit \D* to \D+:

/(?=\w{5,})(?=\D+\d{2,})/;

Then it only fails one test, which is the 8Pass99 one. I think that it’s the fact that this current code is allowing numbers to go at the beginning but when I try to use ^ to specify numbers only at the beginning, it fails the “astr1on11aut” test which I think is because that ^ pushes the numbers to the end so it won’t allow the 1 in the middle of that test case. I have tried using both anchor methods as well.

Any suggestions,
Thanks!
Your browser information:

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

Challenge: Positive and Negative Lookahead

Link to the challenge:

putting the ^ at the beginning is a good step

so let’s see why the other one fail:

you have “one or more non numbers followed by two numbers”
instead there should be no limitt about other numbers, only that there should be two consecutive numbers (and no starting with a number)
can you make this less restrictive while keeping the “two consecutive numbers” thing?

1 Like

Glad to be of assistance.

When I first started learning about Regular expressions I found the following links helpful.

https://www.w3schools.com/jsref/jsref_obj_regexp.asp

https://regexlib.com/CheatSheet.aspx

1 Like

Maybe it would be helpful to use a testing tool like https://regex101.com/ it tries to explain what you type. Remember to choose you language first under the hamburger icon.

1 Like