let myString = “Eleanor Roosevelt”;
let myRegex = /(Franklin|Eleanor) (([A-Z].?|[A-Z][a-z]+) )?Roosevelt/;
let result = myRegex.test(myString);
It took me quite a while to figure out why the guide’s solution worked so I thought I would write a breakdown here:
[A-Z] capital letter
. used to represent a literal dot character. (The backward dash takes special properties away from special characters: e.g. \ is used for a literal back slash character.)
? means that previous element is optional. In this casee, there could be a physical dot, or there could not be (perhaps in a middle name like Franklin D. Roosevelt)
| means “or”
[A-Z][a-z]+ A capital letter, followed by one or many (+) small letters
Then there is a space to account for the space between First name and/or Middle name and Surname
Please correct me if I’m wrong and also I still don’t understand why there is a question mark before Roosevelt. Surely the first/middle name isn’t optional?