Regex Zero or More Times: How the * Quantifier Works

What the regex * quantifier matches, how greedy, lazy and possessive forms differ, why empty matches surprise people, and how to check a pattern before relying on it.

In regular expressions, "zero or more times" is written with the asterisk, *. It tells the engine that the item right before it may appear any number of times, including not at all. This guide covers what that quantifier changes in a match, how its variants differ, where it causes surprises, and how to check that a pattern behaves the same way where you plan to use it.

What the * quantifier actually does

The asterisk applies to the single preceding token. That token can be a literal character, a character class such as [0-9], the dot, or a parenthesized group. It does not apply to everything typed before it. In the pattern ab*c, only the b is repeated: the a and the c must each appear exactly once.

Pattern: ab*c

"ac"      -> match (zero b)
"abc"     -> match (one b)
"abbbbc"  -> match (four b)
"adc"     -> no match (d is not b)

To repeat more than one character, group them. The pattern (ab)* repeats the pair ab, so it accepts an empty string, ab, abab and so on. Without the parentheses, ab* would repeat only the b.

The asterisk is shorthand for the counted quantifier {0,}, meaning a minimum of zero and no maximum. The two forms are interchangeable in engines that support counted quantifiers.

A regex is a matching rule, not a text transformation by itself

Applying a pattern does not change your text. Matching only reports which part of the text fits the rule. Text is modified only when the match is fed into an operation such as replace, split or extract. This matters with *, because a quantifier that allows zero repetitions can succeed without consuming any characters, and a replace operation will still act on those empty matches.

Why zero matters: empty matches

Because zero occurrences is acceptable, a pattern built entirely from optional parts can match an empty string at every position. In JavaScript, a global replace with x* on text that contains no x inserts the replacement between every character and at both ends:

"abc".replace(/x*/g, "-")
// result: "-a-b-c-"

Engines differ in exactly how they step past an empty match, so the same pattern can give slightly different results in another language or tool. If a pattern must find at least one occurrence, use + (one or more) instead of *, or anchor the pattern so it cannot match nothing.

Greedy, lazy and possessive forms

By default, * is greedy: it takes as many repetitions as it can, then gives some back only if the rest of the pattern would otherwise fail. Adding a question mark, *?, makes it lazy, so it takes as few as possible and expands only when needed. The difference shows up most with the dot:

Text:    <b>bold</b>

<.*>     -> "<b>bold</b>"   (greedy: runs to the last >)
<.*?>    -> "<b>"           (lazy: stops at the first >)

Some engines also support a possessive form, *+, which takes as many repetitions as possible and never gives any back. It can prevent wasted backtracking, but it can also make a pattern fail where the greedy version would succeed. Possessive quantifiers are available in engines such as PCRE and Java, and in Python's re module from version 3.11. JavaScript's built-in regular expressions do not support them. Check your engine's documentation before relying on this form.

Choosing the right quantifier

Syntax | Meaning | Matches nothing? | Typical use
• | Zero or more, greedy | Yes | Optional runs such as trailing spaces
• | One or more, greedy | No | Requiring at least one digit or letter
? | Zero or one | Yes | A single optional character, such as the s in https?
{0,} | Same as * | Yes | Rarely needed; mirrors the {min,max} style
*? | Zero or more, lazy | Yes | Shortest span between two delimiters
*+ | Zero or more, possessive | Yes | Performance-sensitive patterns, where supported

Common mistakes with *

  • Expecting * to repeat a whole word: cat* matches ca, cat and cattt, not catcat. Use (cat)* for that.
  • Confusing regex * with the wildcard * in file globs and search boxes: in a glob, * already means any run of characters, while in a regex the equivalent is .* (the dot followed by the asterisk).
  • Using .* where a narrower class fits: [^"]* stops at the next quote, whereas .* may overreach to a later one.
  • Nesting quantifiers such as (a*)* or (a+)*: on input that almost matches, these can cause very slow backtracking in engines that use a backtracking approach.
  • Starting a pattern with *: many engines, including those in Python and JavaScript, report an error because there is nothing to repeat. In POSIX basic regular expressions, a leading * is treated as a literal asterisk instead.
  • Forgetting that a pattern made only of optional parts can match an empty string, which affects replace, split and validation logic.

Will the pattern work where you paste it?

A pattern that works in one tester is not guaranteed to work the same elsewhere. The meaning of * is broadly consistent, but these details vary between regex flavors and environments:

  • Flavor differences: lazy and possessive suffixes, the meaning of the dot across line breaks, and the handling of empty matches all depend on the engine.
  • POSIX basic syntax, used by some command-line tools by default, treats some characters differently from the extended and Perl-style syntax that most testers use.
  • String escaping: when a pattern sits inside a quoted string in source code, backslashes often need to be doubled unless the language offers raw strings or regex literals.
  • Silent character changes: word processors, chat apps and some editors may replace straight quotes with curly ones or insert non-breaking or zero-width spaces. These look identical on screen but change what the pattern matches. Pasting into a plain-text editor first, or retyping the pattern, avoids this.
  • Unicode handling: whether the dot or a character class treats an emoji or an accented letter as one unit depends on the engine and its flags.

The reliable check is to run the pattern against representative sample text in the same language, library or tool where it will be used, including edge cases such as empty input and text with no matches.

Readability and accessibility of patterns

Dense patterns made of symbols are hard to scan visually and awkward to follow when read aloud by assistive technology. When documenting or sharing a pattern, show it in a code block so its spacing and characters are preserved, and add a short plain-language description, for example: "one letter a, then any number of b, then c." Some engines also offer a verbose or extended mode that allows whitespace and comments inside a pattern, which helps with longer expressions.

Using online regex testers with sensitive text

Some web-based testers process input in the browser, while others may send it to a server or save it in a shareable link. Unless a tool's documentation clearly says input stays on your device, do not paste passwords, API keys, personal data or confidential logs into it. Replace real values with realistic dummy text, or test locally in your own language runtime or editor.

Limits of this guide

This guide describes the general, documented behavior of the * quantifier and its common variants. It does not report test results from any specific website, app or library version. Flavor-specific details, especially empty-match stepping, possessive support and Unicode handling, should be confirmed against the documentation of the engine you use and checked with your own sample text.