Match Characters that Occur Zero or More Times - I just don't get how this works

Tell us what’s happening:

First of all, I don’t really need help in solving this challenge because I’ve already passed it through trial and error. I just want to know how it works.

I can’t really wrap my head around the explanation given in the challenge. It says that using the asterisk “matches characters that occur zero or more times”. I just can’t comprehend how it works. Does that mean you would get a match even if the character you’re searching for is not present, since the asterisk matches characters even if they occurred zero times? Idk if I’m even making sense right now but that’s how I understand it based on the explanation, and I know it can’t be right based on the given examples.

I’m usually quite confident of my English comprehension skills despite being a non-native speaker, but I’m not so sure now lol. The phrase “zero or more times” is really confusing the hell out of me and I can’t proceed to the rest of the challenges because of this. Help please. :sob:

Your code so far


let chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
let chewieRegex = /change/; // Change this line
let result = chewieQuote.match(chewieRegex);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/match-characters-that-occur-zero-or-more-times

Does that mean you would get a match even if the character you’re searching for is not present

That’s exactly right.

For example, /xy*z/ would match xz or xyz or xyyz and so on.

If you’d like a nice visual aid to check your regular expressions you can use a site like RegExr.

1 Like