JavaScript RegExp * Quantifier

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Zero or more

What You’ll Learn

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

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.

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.

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.

📝 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.

⚡ Quick Reference

GoalPattern / API
Zero or more of tokentoken*
Flexible middleab*c
Leading whitespace/^\s*/
Lazy minimal match*?
Longhand form{0,}
Need at least oneUse + instead

📋 * 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)

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.

JavaScript
const tokens = ["ac", "abc", "abbbc", "abxc"];

tokens.forEach((t) => {
  console.log(t, /ab*c/.test(t));
});
// ac    true
// abc   true
// abbbc true
// abxc  false
Try It Yourself

How It Works

b* greedily consumes every b before the final c must match. Zero b characters is valid, so ac passes.

📈 Practical Patterns

Trimming, wildcards, and repeated sequences.

Example 2 — Trim Leading Spaces with ^\s*

Remove zero or more whitespace characters from the start.

JavaScript
const inputs = ["hello", "   hello", "\t\thello"];

inputs.forEach((s) => {
  console.log(JSON.stringify(s), "→", JSON.stringify(s.replace(/^\s*/, "")));
});
// "hello"     → "hello"
// "   hello"  → "hello"
// "\t\thello" → "hello"
Try It Yourself

How It Works

\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
Try It Yourself

How It Works

This is the core difference from +. Use * when absence is valid; use + when you require at least one character.

Example 4 — Greedy * vs Lazy *?

Compare maximal and minimal wildcard matching.

JavaScript
const text = "say hello. then bye.";

console.log(text.match(/.*\./));   // greedy — to last dot
console.log(text.match(/.*?\./));  // lazy — to first dot
Try It Yourself

How It Works

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.

JavaScript
const laughs = ["", "ha", "haha", "hahaha", "ho"];

laughs.forEach((s) => {
  console.log(JSON.stringify(s), /^(ha)*$/.test(s));
});
// ""      true
// "ha"    true
// "haha"  true
// "hahaha" true
// "ho"    false
Try It Yourself

How It Works

(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.

🚀 Common Use Cases

  • Flexible separatorsab*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.

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.
  • Precedenceab*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.

📝 Notes

  • * is shorthand for {0,}—zero to unlimited repetitions.
  • Previous topic: ? zero-or-one quantifier.
  • Next topic: ^ start anchor.
  • See + when at least one repetition is required.
  • See \s for whitespace with * trimming patterns.
  • Lazy forms: +?, ??, and *? prefer shorter matches.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
* Excellent

Bottom line: Safe everywhere. * is the standard way to match zero or more of a token.

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.

💡 Best Practices

✅ Do

  • Use * when zero repetitions is valid
  • Use ^\s* / \s*$ for edge trimming
  • Try *? for minimal wildcard matching
  • 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)*

Key Takeaways

Knowledge Unlocked

Five things to remember about the RegExp * quantifier

Match zero or more repetitions with confidence.

5
Core concepts
📈 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.

Continue to ^ start anchor

Learn how the caret matches the beginning of a string or line.

^ anchor tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful