The * quantifier: zero or more, including nothing at all
* means zero or more repetitions of the preceding token, and it is greedy. Because zero is allowed, a pattern made only of starred tokens succeeds everywhere and matches nothing.
At a glance
- Syntax
- *
- Equivalent to
- {0,}
- Greedy
- yes, the lazy form is *?
- Can match empty
- yes, which is the source of most surprises
- Applies to
- the single preceding token or group, not the whole pattern
- Global match on an empty capable pattern
- produces an empty match at every position it cannot fill
In detail
The permissiveness is the whole story. Because zero repetitions satisfy the quantifier, /a*/ succeeds at every position in every string, including at the end. Matching it globally against "abc" returns four results: the run of a at the start, then an empty string before b, before c and at the end of the input.
That behaviour turns a global replace into an insert. Replacing /a*/g in "aaab" with a marker gives "XXbX", because the pattern matched the run of a characters, then matched nothing before b, then matched nothing at the end. Nobody writes that on purpose, and it is a common outcome of a generated pattern where one of the pieces turned out to be optional.
The other consequence is validation. /^\d*$/ accepts an empty string, so a required field checked with a starred quantifier is not required at all. Whenever the answer to at least one is yes, the quantifier should be a plus, and the difference between the two characters is the entire check.
The star binds to exactly one preceding token. In /ab*/ it applies to the b alone, so the pattern is an a followed by any number of b characters. Applying it to a sequence means grouping the sequence first, and a non capturing group is the right one unless you also want the text back.
Examples
"abc".match(/a*/g); // ["a", "", "", ""]
"aaab".replace(/a*/g, "X"); // "XXbX"
/^\d*$/.test(""); // true, an empty value passes"abab".match(/ab*/g); // ["ab", "ab"]
"abab".match(/(?:ab)*/g)[0]; // "abab"Things that catch people out
- /^\d*$/ accepts an empty string, so a field validated this way is optional whatever the label next to it says. Use + when at least one character is required.
- A global replace against a pattern that can match empty inserts the replacement at every position it matched nothing, which produces stray characters rather than an error.
- The star applies to the token immediately before it. /ab*/ is not ab repeated, and reading it that way is the usual cause of a pattern that matches far less than expected.
Try it: Regex Tester
Type a pattern and a test string, watch every match highlight as you go, read a plain English breakdown of each token, and check the numbered and named groups it captured.
Regex Tester
LOCAL · Browser-OnlyTest and debug regular expressions with live matching and highlights
Related Regex syntax
All regex syntax →The + quantifier: one or more, and when to prefer it
The plus quantifier requires at least one repetition, which is what makes it the right default for validation where the star quietly accepts nothing.
Lazy quantifiers: *?, +?, ?? and {n,m}?
A trailing question mark makes a quantifier prefer the shortest match. It fixes the overreaching tag pattern, and a negated class often fixes it better still.
Catastrophic backtracking: why a regex freezes a page
Nested quantifiers over overlapping characters make the work grow exponentially with input length. Here is the shape that causes it and the rewrites for it.
The ? quantifier: optional, and its two other jobs
A question mark after a token makes it optional. The same character also marks a lazy quantifier and opens a special group, which is why patterns look dense.
{n}: matching exactly n repetitions
An exact count is the clearest quantifier there is, until it is applied to a group, at which point the capture keeps only the final iteration.
{n,}: at least n repetitions, with no upper bound
The open ended count covers everything * and + cannot express, and a stray space after the comma turns the whole quantifier into literal text.
Tools for this
- Regex Tester Free online regex tester - test and debug regular expressions with live matching and highlights
- Regex Playground Regex playground with live match highlighting, named groups, find and replace, and shareable links