Regex with spaces in the Pipe ("|") search versus no spaces

Just a bit confused why one is working and the other is return an error because this REGEX is killing me. :man_facepalming:t6:

Getting X for :

  • Your regex `myRegex` should return `true` for the string `Franklin D. Roosevelt

Working:

  • let myRegex = /^(Franklin|Eleanor)(\s | [A-Z]\.)?\sRoosevelt/g; // Change this line

Failing to Match:

  • let myRegex = /^(Franklin|Eleanor)(\s|[A-Z]\.)?\sRoosevelt/g; // Change this line

The “Working” passes the lesson.
Just that there does not appear to be consistancy with the REGEX as perfect example: the ^(Franklin|Eleanor) does not have a space but it is working fine.

Any help / clarification would be greatly appreciated.

Thank you in advance!!

Your code so far


let myString = "Franklin D. Roosevelt";
let myRegex = /^(Franklin|Eleanor)(\s | [A-Z]\.)?\sRoosevelt/g; // Change this line
let result = myRegex.test(myString); // Change this line
let print = myString.match(myRegex);
// After passing the challenge experiment with myString and see how the grouping works
console.log(result);
console.log(print);

Your browser information:

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

Challenge: Check For Mixed Grouping of Characters

Link to the challenge:

First, you want to use the test method on the regex for this challenge (see the example in the instructions).

Second, your failing regex is not working because you are doing “either a space OR a letter followed by a dot”.

(\s|[A-Z]\.)?\sRoosevelt

So if you have the string “Franklin D. Roosevelt” then it will match the space after “Franklin” and then immediately look for another space and then “Roosevelt”, so you have left no room for the middle initial.

1 Like