Help with Regex asterisk "*"

Heya all!!

I’m having trouble with regular expressions of Javascript section and I came across this lesson:


I have googled a bit about what this “*” do and I still can’t get why it returns “g” on the second string (there are actually two "g"s on that string). And why the third string returns “null”, when yes, “g” occurs zero times, but “o” does occur… and as I see is not looking for the string “go” nor for “g” alone, but for both characters separately, right? Otherwise why it returns “gooooooooo” in the first one?

Also, If I run this code on Visual Studio and check the console, it actually returns an array…


I used console.log() cause without that, it was not returning anything at all on the console…

Thanks in advance to anyone who is wiling to help!


If you don’t use the g (global) flag, it stops at first match

It is because the regex matches “a g followed by 0 or more o”

Yes, match() returns an array

1 Like

Hi! Thank u for your time answering!
Got a question regarding it. If the regex matches “a g followed by 0 or more o” as you say, why it returns “g” alone on the second string? There’s no g followed by o there.

There is a g followed by 0 o, the asterisk means zero or more.
That matches “g” (zero o), “go”, “goo” etc

1 Like

Thank you very much!! :smiley:
So in a string it will look for al the characters together and check if the last one appears zero or more times?

That’s not a general thing, it’s * that means 0 or more times

So if you have /mu*m/ it matches “mm” because the u is set to appear 0 or more, it also matches “mum” and “muuuuum”

1 Like

Thank you very muchhhh! Lov ya!! :smiling_face_with_three_hearts: