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.
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.
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”.
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.
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.
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.
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.
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.
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!
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.
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.
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:
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.
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?