I don't understand what does * exactly does on RegExp

Tell us what’s happening:
Hi, solution says that using * makes possible to check for middle names, but I don’t get it. In a previous challenge it is said that * “matches characters that occur zero or more times”… what does that mean? Thank you!

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/84.0.4147.105 Safari/537.36.

Challenge: Check For Mixed Grouping of Characters

Link to the challenge:

Hello!

It means exactly that :slight_smile:. The thing you’re missing here is the preceding dot (.). A dot followed by an asterisk means match anything zero or more times, be it a character, a number, a symbol and, depending on the flags used on the regex, it may match non-printable characters (like new lines).

1 Like

It allows for more flexibility in your pattern. Let’s say that you only care that the string has the word “Hello” followed by the word “World” (so you would be happy with "HelloWorld" or "Hello World"). Then you could use the pattern /Hello\s*World/.

2 Likes