Regular expressions JAVASCRIPT

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.

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:

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?

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

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:

and

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

1 Like

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

1 Like