Anon
Anon

Reputation: 109

Regex asterisk usage

According to MDN x* : *Matches the preceding item x 0 or more times.*

Essentially the preceding characters should be completely optional. The string will be matched whether they exist or not. So why is it that:

1.

var text  = "foobar";
var re    = /q*/;
var found = text.match(re);
console.log(found); // prints '["", index: 0, input: "foobar"]'

but

var re    = /qz*/;
console.log(found); // prints 'null'

Both expressions equally don't exist and thus should be matched 0 times and '""' should be returned.

Or:

2.

var re    = /fz*/;
console.log(found); // prints '["f", index: 0, input: "foobar"]'

but

var re    = /fzq*/;
console.log(found); // prints 'null'

What's happening here? From my understanding 'fzq' doesn't exist so should be matched 0 times, and '""' should be returned, right? And if it somehow matches on per character basis instead of the whole string, 'fzq*' should return the same result as 'fz' - 'f' is matched once and the rest is matched 0 times. But apparently this is not what's happening.

Could someone shed some light on what the hell is happening here?

Upvotes: 3

Views: 3492

Answers (1)

Tushar
Tushar

Reputation: 87203

x* : Matches the preceding item x 0 or more times.

You misunderstood this statement with everything before * is matched zero or more times.

When you match q*, the regex will search for q in the input string. As there is no q in the foobar, it'll return an empty string.

When you try to match qz*, the regex will try to search for any number of zs after q. As there is no q in the input string, it'll return null.

To match more characters by * or any other quantifiers, you need to group them using parenthesis.

(qz)*

which means, match the string qz zero or more times.

To understand a regex, you can use https://regex101.com/ site.

Upvotes: 4

Related Questions