In the example in the left pane

Continuing the discussion from freeCodeCamp Challenge Guide: Match Characters that Occur Zero or More Times:

let soccerWord = "gooooooooal!";
let gPhrase = "gut feeling";
let oPhrase = "over the moon";
let goRegex = /go*/;
soccerWord.match(goRegex);
gPhrase.match(goRegex);
oPhrase.match(goRegex);

In order, the three match calls would return the values ["goooooooo"] , ["g"] , and null .

…why does gPhrase only return the first ‘g’ of ‘gut feeling’ and not the second one please?

Thanks.

1 Like

Because the regex

/go*/

match only the g in "gut feeling"

Notice that the regex /go*/ means find something with g and then o if theres any (zero or more). There’s no o in "gut feeling"

If you change your gPhrase to got feeling

then it will returns go

Thank you

I already understand that, I was asking why it doesn’t match the ‘g’ in ‘feeling’.

Sorry i misunderstood your question. It’s because the regex doesn’t have global flags, if you want the program to return each time the regex matches you’ll have to do this

let goRegex = /go*/g

read more about flags

1 Like

Ah of course.

Thank you again.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.