Check For Mixed Grouping of Characters -JS

This Code does bypass all of the tests.
But I would like to know if there s any other way to implement

Franklin D. Roosevelt without adding (Franklin | Franklin D.) both and just by adding one Franklin and a “D.” somewhere?

Your code so far


let myString = "Eleanor Roosevelt";
let myRegex = /(Franklin|Eleanor|Franklin D.)\sRoosevelt/; // Change this line
let result = myRegex.test(myString); // Change this line
// After passing the challenge experiment with myString and see how the grouping works

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36.

Challenge: Check For Mixed Grouping of Characters

Link to the challenge:

You can use a question mark to test for something that may or may not be there. Here’s an example:

/hello world?/ would match hello world and hello worl
/hello( world)?/ would match hello and hello world

2 Likes

Did you see the “Hints” page for this challenge?

Spoiler Alert - They did not hide the solution on that page, it is openly viewable in plain text. Don’t click it if you don’t want FCC solution yet!

but Hint 3 on that page is:

Hint 3

Use .* to allow for middle names.

. Which means “any single character”
*Which matches any string that contains zero or more occurrences of n ?

Have you considered Using .* to allow for middle names. Where matches any single character before “Roosevelt”?

Or where you looking for some totally different solution?

2 Likes

Thank you so much.

let myRegex = /(Franklin|Eleanor)\s?D?.?\sRoosevelt/;

This code did work for me well and good.

It may have passed the tests, but a couple of issues with that regex…

  • The period is not escaped. So it is checking for any character, which includes a period. Change it to \. to match a literal period.
  • Each character before your question marks is optional, so it would match FranklinD Roosevelt and Franklin DQ Roosevelt

You can put a group in parenthesis with a question mark after it to match the whole group or not. So I might have done something like this: /(Franklin|Eleanor)(\s\w\.)?\sRoosevelt/
It would match the first name, then (space, character, period) only if all three are there, and last name. Hope that makes sense and helps a little.

4 Likes

This looks much clean.
Understood your point.
Thank you.

Yeah, I was looking for different possible answers, as my code looked a bit dizzy and unsatisfying.
Got your point, of using .* which means 0 or more dots, ryt?
Thank you.

I think it means basically any character between the group and Roosevelt. So I think it is a very loose (greedy) way to do it? But it was as in the “Official FCC Solution”.

@moT01’s way seems more precise.

I am still fuzzy with and learning the regex syntax myself. The concept makes sense, but the syntax is kinda tricky and complicated to me when building up a pattern from scratch.

2 Likes

Oh right, .* means any character, zero or times.
it’s a bit tough for me,
Going through regex for first time. Hopefully, things should get familiar with time.

1 Like

Hi! I’m late to this post, but I have figured out an alternative way that might be useful as experience to solve the challenge.

I only used what I’ve learned through FCC Regex challenges so my solution is something like this: /(Franklin|Eleanor)\s*\w*\W*\s*Roosevelt/

Basically I’m saying:

  • List item
    (Franklin|Eleanor) → Search a string like Franklin or Eleanor;
    \s* → It can contain 0 or more whitespaces;
    \w* → It can contain 0 or more Alphanumeric characters;
    \W* → It can contain 0 or more Non-Alphanumeric Characters;
    \s* → It can contain 0 or more whitespaces;
    Roosevelt → Search a string Roosevelt ;

I know that my solution would match something like: Franklin D… Rooselvet or Franklin D-. Rooselvet, but it is only a way to get more knowledge in Regex.

Regards to all!

5 Likes

As I understand, your solution will also pass this string “EleanorRoosevelt” (a name without space between first name and last name) or even “FranklinD.Roosevelt” because the number of spaces can be 0 due to \s*. It shouldn’t, right? Correct me if I’m wrong.

1 Like

son.vu you are right, his version accepts any amount of white spaces because of the \s. See, you already get the grip on regexps. :slight_smile:

Here is the version that I would use:

/(Eleanor|Franklin) (.* )?Roosevelt/

This regexp ensures that there is a whitspace between first name, middle name (if existing) and last name.

But you may also break it with two middle names:

“Eleanor FoobarFranchise Roosevelt”

I always try to find the simplest regexp that solves my problem. If you know that Persons in your program have only one middle name, then my solution works. If they may have multiple middle names, you need to further improve on that.

Rule of thumb: keep your regexps as simple as possible!

best
Dennis

5 Likes

son.vu, you are right! As you and gdennci said, my solution will also pass a string without space between first and last name due to \s*; I know this isn’t the best Regex expression, but I found this solution because I was trying to solve the challenges only using what I learned through FCC regex challenges. :slightly_smiling_face:

A workaround to fix the 0 or more white spaces (\s*) between first and last name is using + (\s+) instead of *. Basically this is saying that between first and last name it should contain at least 1 white space.

Regards to all!

1 Like

Yes, I found that middle part difficult, but ultimately, it comes down to “zero or more characters.” So:

/(Franklin|Eleanor).*Roosevelt$/;

The period (wildcard) and asterisk (zero or more) account for any characters between the first name and last, such as middle initials or a middle name. But although this is literally be the final answer (and will pass the test), it does not account for instances where additional letters are added to the first name (for instance, “Eleanorant” or “Frankliner”). If you want to test for that, you have to add a space test to make sure there’s a space directly after the first name:

/(Franklin/Eleanor)\s.*Roosevelt$/;

1 Like

Your solution is neat and clean, I went for a long way :joy:
let myRegex = /(Eleanor|Franklin) Roosevelt|(Eleanor|Franklin) D. Roosevelt/;

I came up with this : let myRegex = /(Franklin|Eleanor) (.* )?Roosevelt/
(.
)*? allows me to cover multiple middle names shortened or not, combinations of shortened and full middle names or no middle names at all.
What do you think of my solution? I hope this could help somebody.
Sorry if my english is weird, I try my best. :sweat_smile:

Thanks for the explanation. My regex ended up being long and specific to pass the test. I was confused at how FCC’s regex still passed test 3 even if you use the /i flag?

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