# Regular Expression: Homework

**URL:** <https://group.classcentral.com/t/regular-expression-homework/35722>\
**Category:** JavaScript Bootcamp\
**Created:** [September 19, 2022, 9:44am UTC](https://group.classcentral.com/t/regular-expression-homework/35722 "2022-09-19T09:44:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![KikiDotPy](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/kikidotpy/32/4910_2.png) [@KikiDotPy](https://group.classcentral.com/u/KikiDotPy)\
**Post date:** [September 19, 2022, 9:44am UTC](https://group.classcentral.com/t/regular-expression-homework/35722/1 "2022-09-19T09:44:36Z")

</div>

[@everyone](https://group.classcentral.com/groups/everyone)

Hey **JavaScripters!**

Such a great Monday morning, folks! Seeing today’s teamwork was a JOY! 💜 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!

📢 **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!

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures)**
>
> Learn to Code — For Free

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🤚

---

<div class="post-metadata">

**Author:** ![robiul121](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/robiul121/32/1935_2.png) [@robiul121](https://group.classcentral.com/u/robiul121)\
**Post date:** [September 19, 2022, 5:03pm UTC](https://group.classcentral.com/t/regular-expression-homework/35722/2 "2022-09-19T17:03:08Z")

</div>

![image](https://us1.discourse-cdn.com/flex016/uploads/ccstudygroup/original/2X/7/72ff2560b8035e2a5a4d7ff692489e7daa625bc0.png)

\*\*

Done! 😊

\*\*

---

<div class="post-metadata">

**Author:** ![Jane2963637](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/jane2963637/32/6117_2.png) [@Jane2963637](https://group.classcentral.com/u/Jane2963637)\
**Post date:** [September 21, 2022, 1:30am UTC](https://group.classcentral.com/t/regular-expression-homework/35722/3 "2022-09-21T01:30:50Z")

</div>

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.

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![Tzerio](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/tzerio/32/6142_2.png) [@Tzerio](https://group.classcentral.com/u/Tzerio)\
**Post date:** [September 21, 2022, 8:25am UTC](https://group.classcentral.com/t/regular-expression-homework/35722/4 "2022-09-21T08:25:47Z")

</div>

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]`

---

<div class="post-metadata">

**Author:** ![stevenr2945563](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@stevenr2945563](https://group.classcentral.com/u/stevenr2945563)\
**Post date:** [September 27, 2022, 7:02pm UTC](https://group.classcentral.com/t/regular-expression-homework/35722/5 "2022-09-27T19:02:35Z")

</div>

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

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![syllie](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/syllie/32/6257_2.png) [@syllie](https://group.classcentral.com/u/syllie)\
**Post date:** [September 28, 2022, 6:35am UTC](https://group.classcentral.com/t/regular-expression-homework/35722/6 "2022-09-28T06:35:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![stevenr2945563](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@stevenr2945563](https://group.classcentral.com/u/stevenr2945563)\
**Post date:** [September 28, 2022, 8:02am UTC](https://group.classcentral.com/t/regular-expression-homework/35722/7 "2022-09-28T08:02:51Z")

</div>

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`:

```auto
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`:

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

```

---

<div class="post-metadata">

**Author:** ![Tzerio](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/tzerio/32/6142_2.png) [@Tzerio](https://group.classcentral.com/u/Tzerio)\
**Post date:** [September 28, 2022, 8:48am UTC](https://group.classcentral.com/t/regular-expression-homework/35722/8 "2022-09-28T08:48:31Z")

</div>

Let’s look at this with match():

```auto
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

---

<div class="post-metadata">

**Author:** ![syllie](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/syllie/32/6257_2.png) [@syllie](https://group.classcentral.com/u/syllie)\
**Post date:** [September 28, 2022, 11:53am UTC](https://group.classcentral.com/t/regular-expression-homework/35722/9 "2022-09-28T11:53:54Z")

</div>

Just one more thing to add perhaps, not sure if you are aware of [https://regex101.com/](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?

---

<div class="post-metadata">

**Author:** ![tylers2924003](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@tylers2924003](https://group.classcentral.com/u/tylers2924003)\
**Post date:** [October 20, 2022, 5:49pm UTC](https://group.classcentral.com/t/regular-expression-homework/35722/10 "2022-10-20T17:49:57Z")

</div>

Thank you for this explanation, I was stuck on this as well but your explanation makes sense. 😁

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/ccstudygroup/original/1X/36206832181ed26910a18e33d8bfef087cf661b1.svg) [@system](https://group.classcentral.com/u/system)\
**Post date:** [December 4, 2022, 5:50pm UTC](https://group.classcentral.com/t/regular-expression-homework/35722/11 "2022-12-04T17:50:39Z")

</div>

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.
