Regular Expression: Homework

@everyone

Hey JavaScripters!

Such a great Monday morning, folks! Seeing today’s teamwork was a JOY! :purple_heart: Thank you for keeping with me with Regex I know how frustrating it can be; play around with it and take time to let the information sink in, this bootcamp focus on letting you introduce to these topics and you don’t need to know everything right away!

:loudspeaker: Quick reminder about homework for the next session!
Tomorrow we’ll start with our first project Palindrome Checker, so your homework is to finish the Regex section!

Remember folks, you’re not alone.
Don’t hesitate to seek help in the #js-help channel or on the forum!

Be sure to collaborate, stay hydrated and get plenty of rest!!!

We’re all in this together!
Happy Coding!
#PatPat🤚

4 Likes

**

Done! :blush:

**

6 Likes

Hi, I have a little question after watching today’s (Tuesday’s) Q and A:

I used brackets in my Restrict User Names exercise. Is this not recommended? I can see from today’s class it is not necessary, but it helps me group my thoughts.

let username = "JackOfAllTrades";

let userCheck = /(^[a-z][a-z]+)(\d*$)|(^[a-z])(\d\d+$)/i; // Change this line

let result = userCheck.test(username);

console.log(result);
3 Likes

Hello!
I think it doesn’t matter if you use test, but if you use match you might get some results that you didn’t intended to have
let result = username.match(userCheck);
gives the output:
[ 'JackOfAllTrades123', 'JackOfAllTrades', '123', undefined, undefined, index: 0, input: 'JackOfAllTrades123', groups: undefined ]

instead of only one match without the brackets
[ 'JackOfAllTrades123', index: 0, input: 'JackOfAllTrades123', groups: undefined ]

4 Likes

I am a bit confused about the solution to “Positive and Negative Lookahead.” I initially tried this:

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

And this did not work. Looking at the hint, it seems that the solution needs to be this:

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

where there is an extra \w* in the second lookahead. This means that there should be 0 or more alphanumeric characters preceding the two digits. But why is that required to be specified?

Strangely, my initial attempt matched for sampleWord = "astro11naut"; but not for let sampleWord = "astronaut11";. I’m having trouble seeing why it should succeed for one but not the other.

1 Like

Hi Steven,

I think it is important to understand that at the end of a lookahead, the regex engine hasn’t moved on the string. It searches the string to see if the pattern you look for is there, but it does not match it. So the first part of the look up tests ‘are there at least 6 characters’, and the second need to determine ‘if there are 2 consecutive numbers’. For the second part, that means there can be other text preceding it, but we are satisfied when we have found 2 digits following that. So the pattern needs to account for starting characters, but does not care if there is more following it. hence the \w*\d{2} pattern.

I hope that helps?

I do not know what tripped you up with the use of let/no-let, could you post a full code snippet so I can verify?

1 Like

Sorry, Syl, still not grokking it. In my example, the issue wasn’t with let; I inadvertantly dropped the first one in my post. The issue is with where the double digits fell in sampleWord. So, in this version, result is true:

let sampleWord = "astro11naut";
let pwRegex = /(?=\w{6,})(?=\d{2})/; // Change this line
let result = pwRegex.test(sampleWord);
console.log(result);

But in this case, result is false:

let sampleWord = "astronaut11";
let pwRegex = /(?=\w{6,})(?=\d{2})/; // Change this line
let result = pwRegex.test(sampleWord);
console.log(result);
1 Like

Let’s look at this with match():

let sampleWord = "astro11naut";
let pwRegex = /(?=\w{6,})(?=\d{2})/; // Change this line
let result = sampleWord.match(pwRegex);
output: [ '', index: 5, input: 'astro11naut', groups: undefined ]

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

output: null

When you compare the outputs, you can realize, that your regex actually tries to find an index in the string that is followed by 6 letters or numbers AND both the first and the second characters following that index have to be numbers

But you want it to match at least 6 characters AND there have to be 2 consecutive numbers but it doesn’t matter whether these are at the beginning of the match or at the end or anywhere between

That’s why the solution puts there an extra \w* for the optional other chars before the numbers

3 Likes

Just one more thing to add perhaps, not sure if you are aware of https://regex101.com/ ? It provides helpful hints in how the regex is solved at the top right, and in the bottom left you have an option to visualize the solving via a step debugger. See if that gives you some more insights?

1 Like