Noob question about regular expressions curriculum!

Hello, i got stuck in " Regular Expressions: Match Characters that Occur Zero or More Times", There’s actually a line i don’t understand!

let soccerWord = "gooooooooal!";
let gPhrase = "gut feeling";
let oPhrase = "over the moon";
let goRegex = /go*/;
soccerWord.match(goRegex); // Returns ["goooooooo"]
gPhrase.match(goRegex); // Returns ["g"] //the line i don't understand why!
oPhrase.match(goRegex); // Returns null

I just don’t understand why this line gPhrase.match(goRegex); would return “g”?!!

This is the equivalent of matching g OR o and what follows. so on gPhrase it matches g. If you want to match go then: try /(go)*/ that will return a null for gPhrase but it will only return go on soccerWord not gooooooooo.

You can test it on this site: https://regexr.com/

1 Like

The o* aftter g in the regular expression /go*/ means match g then match zero or more os following the g. In this case, it matches the g in gut feeling, because there are no os after g. If the expression would have been /go/, then gPhrase.match(goRegex); would have returned null because there is not an instance of a g followed by an o.