Check For Mixed Grouping of Characters. Minor(I think) clarification needed

This works:


let myString = "Eleanor Roosevelt";
let myRegex = /(Franklin|Eleanor).*Roosevelt/g;
let result = myRegex.test(myString);

but this doesn’t:


let myString = "Eleanor Roosevelt";
let myRegex = /(Franklin|Eleanor)[.*]Roosevelt/g;
let result = myRegex.test(myString);

WHY?

This is a character set, so it matches a single period or a single asterisk.

In the working code, . is a meta-character that matches any character. When you add the * directly after the ., then it tries to match zero or more of any character.

1 Like