Regular Expressions - Check For Mixed Grouping of Characters (Why doesnt this work?)

Tell us what’s happening:
I just don’t understand why this code isn’t working. It checks for Franklin or Eleanor in the beginning, and Roosevelt at the end. I feel like this “should” work.

Your code so far

let myString = "Eleanor Roosevelt";
let myRegex = /(?=[^(Franklin|Eleanor)])(?=[Roosevelt$])/;// 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36

Challenge: Regular Expressions - Check For Mixed Grouping of Characters

Link to the challenge:

I think you’ve overcomplicated this a little with unnecessary lookaheads.
If you simply define the beginning and end of string with ^ and $ (as you have done) and then include (Franklin|Eleanor) and Roosevelt without any of the other parentheses or brackets, you will then only need to account for the imperative space between first and last name and for the possible inclusion of a middle name.

If I have those ^ and $ why do I need to account for the middle? That’s what I’m not understanding.

/^(Franklin|Eleanor)Roosevelt$/

This only precludes strings which have anything before Franklin or Eleanor or anything after Roosevelt.
So ‘123FranklinRoosevelt’ would return false but ‘FranklinRoosevelt’ would return true.
You would need to put something else in the middle of the regex to account for the space and a possible middle name.

I would also like to point out that if putting your caret character ^ inside of the [ and ] at the beginning, you are telling your regex to match anything that is not in your capture group ie the things you have in your parentheses. So if you provided something like A. Roosevelt it would likely match, because A. doesn’t match Franklin or Eleanor. At least that’s the way i read it. I could be wrong about that, but I’ll test it later to make sure I’m providing accurate advice. Igor makes a sound suggestion, don’t over complicate. Keep it simple for these challenges.

Ok. Got it figured out. Thanks peeps. I wanted to really understand it before I moved on.

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