The * quantifier repeats the previous token zero or more times. It is the most permissive repetition operator: the token may appear many times, once, or not at all. Master * for flexible patterns like ab*c, whitespace trimming with \s*, and greedy wildcards.
01
*
Zero or more
02
{0,}
Same meaning
03
Greedy
Max match
04
*?
Lazy mode
05
Empty OK
Zero repeats
06
vs +
Not required
Fundamentals
Introduction
Some parts of text repeat an unknown number of times: multiple spaces, repeated letters, or optional middle sections. The * quantifier says “match this token as many times as you want—including zero times.” That last part distinguishes * from +, which requires at least one occurrence.
Patterns like ab*c match ac (no b at all), abc, and abbbc. Because zero repetitions are allowed, * can also produce surprising empty matches when combined with the global flag—a common beginner pitfall covered later in this tutorial.
Concept
Understanding the * Quantifier
Place * immediately after a character, metacharacter, class, or group. The engine greedily consumes as many repetitions as possible while still allowing the rest of the pattern to succeed.
JavaScript
const words = ["ac", "abc", "abbbc", "abxc"];
words.forEach((w) => {
console.log(w, /ab*c/.test(w));
});
// ac true (zero b)
// abc true
// abbbc true
// abxc false
Shorthand: * equals {0,}. On an empty string, /a*/ still succeeds because matching zero a characters is valid.
💡
Beginner Tip
When you need at least one repetition, use + instead of *. \d+ requires digits; \d* allows none.
Usage
How to Use the * Quantifier
Attach * to tokens that may repeat any number of times, including never:
JavaScript
/ab*c/; // a, zero+ b, c
/^\s*/; // leading whitespace (incl. none)
/(ha)*/g; // zero or more "ha" groups
/.* /; // greedy to last space
/.*?\./; // lazy to first dot
" hi".replace(/^\s*/, ""); // "hi"
Use test() for presence checks, match() to extract runs, and replace() with anchored \s* to trim edges.
Foundation
📝 Syntax
JavaScript
token* // zero or more of token
ab*c // flexible middle section
\s* // zero or more whitespace
(group)* // repeat entire group (or skip)
token{0,} // equivalent to *
token*? // lazy — as few as possible
Common patterns
/ab*c/ — a and c with any number of b between.
/^\s*/ — strip leading whitespace (including none).
/\s*$/ — strip trailing whitespace.
/go*/ — g followed by zero or more o.
//g — lazy match for HTML comments.
Cheat Sheet
⚡ Quick Reference
Goal
Pattern / API
Zero or more of token
token*
Flexible middle
ab*c
Leading whitespace
/^\s*/
Lazy minimal match
*?
Longhand form
{0,}
Need at least one
Use + instead
Compare
📋 * vs + vs ?
How many times the preceding token may repeat.
*
token*
Zero or more
+
token+
One or more
?
token?
Zero or one
On ""
/a*/
Matches (zero a’s)
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. Examples cover flexible middle sections, whitespace trimming, zero vs one-or-more, greedy vs lazy wildcards, and repeated groups.
📚 Getting Started
Allow zero or many repetitions in the middle of a pattern.
Example 1 — Flexible Middle with ab*c
Match a and c with any number of b characters between.
\s* matches zero or more whitespace chars. On strings with no leading space, it matches zero times and replace still succeeds unchanged.
Example 3 — * vs + on Empty Input
See why * allows zero matches but + does not.
JavaScript
const empty = "";
console.log(/a*/.test(empty)); // true — zero a's OK
console.log(/a+/.test(empty)); // false — needs at least one
console.log(empty.match(/\d*/)[0]); // "" — zero digits
console.log(empty.match(/\d+/)); // null
Greedy .* consumes everything up to the last dot. Lazy .*? stops at the first dot. Same lesson as + vs +?, but * also allows matching nothing before the dot.
Example 5 — Repeat a Group Zero or More Times (ha)*
Match repeated laughter sequences, including none.
(ha)* repeats the whole group any number of times. The empty string matches zero repetitions. Contrast with (ha)+, which would require at least one ha.
Applications
🚀 Common Use Cases
Flexible separators — ab*c style patterns with variable middle text.
Whitespace trimming — ^\s* and \s*$ for edges.
Wildcards — .* for “anything” between anchors (use carefully).
Optional repeated sections — (prefix)* for zero or more blocks.
Comment parsing — lazy .*? between delimiters.
Loose token matching — when absence of a token is as valid as many copies.
Watch Out
Important Considerations
Empty matches — /\d*/g on "a1b" returns empty strings between characters; prefer \d+ when you need digits.
Greedy default — .* may span too far; use *? for minimal matching.
Precedence — ab*c repeats only b; use (ab)*c to repeat ab.
Performance — nested * quantifiers on hostile input can cause catastrophic backtracking.
Not a substitute for + — when at least one char is required, * is too permissive.
Anchors help — combine ^ and $ to avoid accidental partial matches.
🧠 How the * Quantifier Works
1
Try zero first (lazy) or many (greedy)
Greedy * starts by matching as many tokens as possible.
Greedy
2
Continue the pattern
The engine tries to match what comes after the quantified token.
Next
3
Backtrack if needed
On failure, give back repetitions one at a time until the rest succeeds—or try zero.
Retry
4
🔢
Report match
Zero repetitions is always a valid path when the rest of the pattern allows it.
Important
📝 Notes
* is shorthand for {0,}—zero to unlimited repetitions.
Lazy forms: +?, ??, and *? prefer shorter matches.
Compatibility
Browser & Runtime Support
The * quantifier is part of core JavaScript regular expression syntax.
✓ Baseline · ES3+
RegExp * quantifier
Supported in every browser and Node.js version. Use * for zero-or-more repetition of any token or group.
99%Universal syntax
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
*Excellent
Bottom line: Safe everywhere. * is the standard way to match zero or more of a token.
Wrap Up
Conclusion
The * quantifier matches zero or more of the previous token—the most flexible repetition operator in regex. It powers patterns from ab*c to whitespace trimming and greedy wildcards.
Remember that zero repetitions is always allowed, watch for empty global matches with \d*, and prefer + when you need at least one. Use *? when greedy matching goes too far. Next, learn the ^ start anchor.
Group tokens when the whole sequence repeats: (ab)*
Prefer + over * when empty should fail
❌ Don’t
Use \d* when you mean “at least one digit”
Ignore empty strings in global * match results
Assume .* stops at the first delimiter (it is greedy)
Nested * on untrusted input without care
Write abc* when you mean (abc)*
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about the RegExp * quantifier
Match zero or more repetitions with confidence.
5
Core concepts
🔢01
*
Zero or more.
Syntax
📈02
{0,}
Same rule.
Shorthand
📄03
Empty OK
Zero repeats.
Key trait
🔒04
*?
Lazy min.
Lazy
🚀05
Use +
Need one+.
Pitfall
❓ Frequently Asked Questions
The * quantifier means zero or more of the preceding token. For example, ab*c matches ac (zero b characters), abc, abbc, and so on. Unlike +, * allows the token to be skipped entirely.
Yes. * is shorthand for {0,}—the preceding item may repeat any number of times, including zero. Both are greedy by default in JavaScript.
+ requires at least one repetition. * allows zero repetitions. So /a+/ fails on an empty string, but /a*/ succeeds with a zero-width match at the start.
* is greedy—it matches as many characters as possible. *? is lazy—it matches as few as possible, often zero first. Use *? when you want minimal repetition.
Because * allows zero digits, the engine can match an empty string at many positions between characters. Use \d+ when you need at least one digit, or filter empty strings from results.
The * quantifier is core JavaScript regex syntax since ES3. It works in every browser and Node.js version without polyfills.
Did you know?
Kleene star is the formal name for the * quantifier in regex theory, named after logician Stephen Kleene. In JavaScript, every repetition quantifier (?, +, *, and {n,m}) builds on the same engine backtracking model that makes zero-length matches possible.