Regex check for mixed grouping of characters help

Tell us what’s happening:
So the problem is to match either Franklin D.Roosevelt or Eleanor Roosevelt with a single regex pattern. I was able to solve this with the following:

Your code so far
See the body below

My original code was

let myRegex =  /(Franklin\sd.|Eleanor)\s\w*/i

This passed the challenge.

Anyhow, I wanted to play around with the code and see how the hints
solved the problem. So their solution was as follows:

let myString = "Eleanor Roosevelt";
let myRegex = /(Franklin|Eleanor).*Roosevelt/i; 
let result = myRegex.test(myString); 

I was interested in this solution, using the wildcard with the asterisk to select
everything in between the first and last name would account for both the
middle initials and any spaces in between. I wanted to try and play around with
regex to see if I could shorten the code and get the same solution,
so I did this:

let myRegex = (Franklin|Eleanor).*\w*/i

This code failed a single test:
Your regex myRegex should return false for the string Franklin Rosevelt.
I thought that was fair, so I decided to change up my code up a bit, and this is
where it gets wierd for me:

let myRegex = (Franklin|Eleanor).*R\w*/i

This also fails the same test.

So next I decide to add a few more characters to see how things go:

let myRegex = (Franklin|Eleanor).*Roo\w*/i

This one passes for some reason. So I think okay…let me try shortening it
again:

let myRegex = (Franklin|Eleanor).*Ro\w*/i

this one fails. I add the asterisk after Ro, I try adding a +,
neither pattern helps me to pass.

So can anyone help me understand this problem and what i’m doing wrong?

Your browser information:

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

Challenge: Check For Mixed Grouping of Characters

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/check-for-mixed-grouping-of-characters

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
Do not include paragraphs of text, or the those become difficult to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

what’s your issue? what do you not understand?

My question is why my modified solution doesn’t work:

[spoiler]

1) FCC Solution:
 let myRegex = /(Franklin|Eleanor).*Roosevelt/i 

2) My solution 1, working: 
let myRegex = /(Franklin|Eleanor).*roo\w*/i

3) My solution 2, NOT working:
let myRegex = /(Franklin|Eleanor).*ro\w*/i 

[/spoiler]

Why does 2 work but 3 fail, the difference between the two is only one 
letter.  The failed test is below:

Your regex myRegex should return false for the string "Franklin Rosevelt."

I think it might be the test cases they have?

For example your solution 2 will let last name “rose” go and solution 1 might fail if they have “root”.

1 Like

thanks! I realized the reason why I kept getting the error

"Your regex myRegex should return false for the string ‘Franklin Rosevelt.’ "

Roosevelt has two O’s, but like you said my solution lets Rosevelt go which is a spelling mistake. Thus it should return false for that test, but my pattern kept returning true.