# Regular expressions JAVASCRIPT

**URL:** <https://forum.freecodecamp.org/t/regular-expressions-javascript/593655>\
**Category:** I Built This\
**Created:** [February 21, 2023, 11:05am UTC](https://forum.freecodecamp.org/t/regular-expressions-javascript/593655 "2023-02-21T11:05:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alrv97](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@alrv97](https://forum.freecodecamp.org/u/alrv97)\
**Post date:** [February 21, 2023, 11:05am UTC](https://forum.freecodecamp.org/t/regular-expressions-javascript/593655/1 "2023-02-21T11:05:44Z")

</div>

Hi, I can’t understand this code, I am triying to give my best to understand it but I can’t. Could someone explain this code for me and the others who can’t undestand? I will be so grateful.  
Thank so much and keep coding  
P.S: What i don’t undestand is the solution, the other code and function yes. Is only the solution.

```javascript
let myString = "Eleanor Roosevelt";
let myRegex = /(Franklin|Eleanor) (([A-Z]\.?|[A-Z][a-z]+) )?Roosevelt/; // Cambia esta línea
let result = myRegex.test(myString); // Cambia esta línea
// Después de pasar el desafío experimenta con myString y observa cómo funciona la agrupación

```

**Información de tu navegador:**

El agente de usuario es: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36`

**Desafío:** Expresiones regulares - Comprueba agrupaciones mixtas de caracteres

**Enlaza al desafío:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/espanol/learn/javascript-algorithms-and-data-structures/regular-expressions/check-for-mixed-grouping-of-characters)**
>
> Aprende a programar, ¡GRATIS!

---

<div class="post-metadata">

**Author:** ![hbar1st](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hbar1st/32/372649_2.png) [@hbar1st](https://forum.freecodecamp.org/u/hbar1st)\
**Post date:** [February 21, 2023, 12:11pm UTC](https://forum.freecodecamp.org/t/regular-expressions-javascript/593655/2 "2023-02-21T12:11:53Z")

</div>

The regex has three parts.  
In general they are:  
1- a capture group that checks for Eleanor or Franklin, followed by one space character  
2- a nested capture group that checks for the presence of a middle name, followed by a space  
3-and Roosevelt

I think you understand 1 and 3?  
So just need help with 2?

---

<div class="post-metadata">

**Author:** ![alrv97](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@alrv97](https://forum.freecodecamp.org/u/alrv97)\
**Post date:** [February 21, 2023, 12:45pm UTC](https://forum.freecodecamp.org/t/regular-expressions-javascript/593655/3 "2023-02-21T12:45:47Z")

</div>

> [@alrv97](#):
>
> `(([A-Z]\.?|[A-Z][a-z]+) `

Yes, correct. What I don’t understand is the second.

---

<div class="post-metadata">

**Author:** ![hbar1st](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hbar1st/32/372649_2.png) [@hbar1st](https://forum.freecodecamp.org/u/hbar1st)\
**Post date:** [February 21, 2023, 1:52pm UTC](https://forum.freecodecamp.org/t/regular-expressions-javascript/593655/4 "2023-02-21T13:52:34Z")

</div>

> [@alrv97](#):
>
> `(([A-Z]\.?|[A-Z][a-z]+) )?`

This is also multiple parts in itself.  
In general the whole thing is a pattern that is to be checked for 0 or 1 times (because of the ? at the end). So either the whole pattern finds a match or it finds no matches.  
Within the pattern there is a capture group followed by one space.  
And within the nested capture group there is an or | which gives us 2 different things to look at  
1-`[A-Z]\.?`  
and  
2- `[A-Z][a-z]+`

1 is looking for a character class A-Z, so this pattern must start with a capital letter as even 2 is doing that also with [A-Z] placed to the leftmost position of the pattern

1 then looks for a dot to be present 0 or 1 times (that is the `\.?`  
while  
2 looks for any number of lowercase letters to follow the upper case one (but must be at least 1 lowercase letter.

So taking both patterns together, we can see it looking for an initial like  
`M.` for eg or a just `M` or a middle name like `Manny`

Some references to help you with regex:

> **[Regular expression syntax cheat sheet - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet)**
>
> This page provides an overall cheat sheet of all the capabilities of RegExp syntax by aggregating the content of the articles in the RegExp guide. If you need more information on a specific topic, please follow the link on the corresponding heading...

and

> **[regex101: build, test, and debug regex](https://regex101.com)**
>
> Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.

(the regex101 site should be set to ECMAScript setting if you want to use it for javascript patterns)

---

<div class="post-metadata">

**Author:** ![alrv97](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@alrv97](https://forum.freecodecamp.org/u/alrv97)\
**Post date:** [February 21, 2023, 4:20pm UTC](https://forum.freecodecamp.org/t/regular-expressions-javascript/593655/5 "2023-02-21T16:20:06Z")

</div>

Wow Thank you so much for your time, the reference and all.  
Have a nice day and thank you for all.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [August 23, 2023, 4:21am UTC](https://forum.freecodecamp.org/t/regular-expressions-javascript/593655/6 "2023-08-23T04:21:05Z")

</div>

Closed on August 23, 2023, 4:21am UTC

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