Search code examples
javascriptregexquantifiers

Javascript regular expression quantifiers: what does it mean to match zero or more times


So I am just trying to clarify what exactly the * quantifier in a javascript regular expression does. The definition from MDN states the following:

x* Matches the preceding item "x" 0 or more times. For example, /bo*/ matches "boooo" in "A ghost booooed" and "b" in "A bird warbled", but nothing in "A goat grunted".

My understanding of this definition is that it will match a "b", and it will match a "b" followed by any number of "o" characters. So would the fact that it matches a "b" character not constitute as 1 match instead of 0? I guess it is the "0 or more times" statement that is throwing me off.


Solution

  • The important phrase in there is "the preceding item". If the preceeding item is a single character, like here, that means that that character can be repeated 0 or more times. bo* will match a b, followed by zero or more os. o* alone will match zero or more os. b(?:oo)* will match b, or boo, or boooo, etc. (zero, or two, or four, or six, ... os)

    Look at the token that immediately precedes the quantifier to see what it's quantifying.

    For another example, [ab][xy]* will match a, or b, or ax, or axxxy, or byxyy. The token preceding the quantifier, [xy], which matches either an x or a y, gets repeated zero or more times.