Explanation of Check For Mixed Grouping of Characters ?Roosevelt

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?

the middle name is optional yes

Ah okay, thanks! I thought the question mark applied to both the first and middle name but now I can see it’s just the middle name

1 Like

FCC is deleting my backslash so it should be " backslash . represents a literal dot character. (The backslash takes special properties away from special characters: e.g. backslash backslash is used for a literal back slash character.)

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