JavaScript RegExp * Quantifier

Beginner
7 min read
Updated: Oct 2026
3 examples
Live preview

What Is the * Quantifier?

Place * after a token to match it zero or more times. It is the most permissive repetition operator: the token may appear many times, once, or not at all. Shorthand for {0,}.

Remember
token*     → zero or more of token
ab*c       → ac, abc, abbbc  (not abxc)
a*         → "", "a", "aaa" all match
a+         → needs at least one a

* is greedy   → match as many as possible
*? is lazy    → match as few as possible
UsePrefer
Zero or more OKtoken*
At least one requiredtoken+
Zero or one onlytoken?

How to Use *

Attach * to the token that may repeat any number of times, including never.

  1. Write the token you want to repeat (character, class, group, or metachar).
  2. Add * right after it — e.g. b*, \s*, (ha)*.
  3. Complete the rest of the pattern so the engine knows when to stop.
  4. If empty must fail, switch to +. For a shorter match, use *?.
PieceRequired?Role
Preceding tokenYesWhat * repeats
* or *?YesZero-or-more; greedy vs lazy
Rest of patternOftenGives the engine a stopping point

Pseudocode

Pseudocode
pattern = <token>* <rest>

// greedy *:
count = as many token matches as possible
while rest fails and count > 0:
    count = count - 1          // backtrack
if rest succeeds (even with count = 0):
    match
else:
    fail

// lazy *? starts at count = 0 and grows only if needed

Cheat Sheet

Keep these facts handy while writing zero-or-more patterns.

TopicDetail
MeaningZero or more of the previous token
Longhand{0,}
Flexible middleab*c → ac, abc, abbbc
Whitespace trim/^\s*/ and /\s*$/
Greedy / lazy* max · *? min
vs +* allows empty; + needs ≥ 1
StandardES3+ (universal)

Live Preview

Pick a pattern and a sample string to see whether * matches (and what match() returns).

Browser demo of the * quantifier — same idea as the examples below.

Result
pattern = /ab*c/
text    = "ac"
test()  → true
match() → ["ac"]

/* b* matched zero times */

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Flexible Middle with ab*c

Match a and c with any number of b characters between — including none.

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

tokens.forEach((t) => {
  console.log(t, /ab*c/.test(t));
});
Try It Yourself

How It Works

  1. Read the pattern. a, then b*, then c.
  2. Zero is allowed. On "ac", b* matches nothing and still succeeds.
  3. Many is fine. On "abbbc", b* takes all three b characters.
  4. Wrong letter fails. "abxc" has x where c is required → false.

Example 2 — * vs + on Empty Input

See why * allows zero matches but + does not.

JavaScript
const empty = "";

console.log(/a*/.test(empty));                      // true
console.log(/a+/.test(empty));                      // false
console.log(JSON.stringify(empty.match(/\d*/)[0])); // ""
console.log(empty.match(/\d+/));                    // null
Try It Yourself

How It Works

  1. Start empty. There are no characters to consume.
  2. a* still matches. Zero repetitions is valid → test returns true.
  3. a+ fails. Plus needs at least one a.
  4. Rule of thumb. Use * when absence is OK; use + when you require content.

Example 3 — Greedy * vs Lazy *?

Compare maximal and minimal wildcard matching up to a dot.

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

  1. Greedy default. .* eats as much as possible, then backtracks to the last ..
  2. Lazy form. .*? expands only until the first . works.
  3. Same idea as +. The ? after a quantifier switches to non-greedy mode.
  4. When to use lazy. Prefer *? when you want the shortest match between delimiters.

Edge Cases & Pitfalls

Check these before relying on * in real projects.

trap

Empty matches with the g flag

/\d*/g on "a1b" can yield empty strings between characters. Prefer \d+ when you need digits.

greedy

.* may span too far

Greedy * matches as much as possible. Use *? when you want the shortest match.

scope

* applies only to the previous token

ab*c repeats b only. Use (ab)*c to repeat the whole group.

choice

Do not use * when you need one+

If empty input should fail, switch to +.

Browser & Runtime Support

The * quantifier is part of core JavaScript regex (ES3+). It works everywhere without polyfills.

✓ Baseline · ES3+

RegExp * quantifier

Supported in every modern and legacy browser, plus all Node.js versions. Universal syntax for zero-or-more repetition.

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. Use * whenever zero repetitions of a token should still succeed.

Performance Notes

Pattern styleCostBest for
token* (simple)Usually linearWhitespace, letters, clear delimiters
.* with anchorsFine in practiceShort strings with a clear end marker
Nested * / ambiguousCan explodeAvoid on untrusted input

Simple * patterns are fast. Trouble starts with nested or overlapping quantifiers that force heavy backtracking. Prefer specific classes (\s*, \d*) over open-ended .* when you can.

Key Takeaways

  • Role: match zero or more of the previous token.
  • vs +: use * when empty is valid; use + when you need at least one.
  • Greedy vs lazy: * takes the most; *? takes the least.
  • Scope: ab*c repeats only b — group with (ab)* when needed.

One line: * means “zero or more” — flexible, greedy by default, and too loose when you actually need +.

Frequently Asked Questions

The * quantifier means zero or more of the preceding token. For example, ab*c matches ac (zero b characters), abc, and abbbc. 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. 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 while still letting the rest of the pattern succeed.
Because * allows zero digits, the engine can match an empty string at many positions. Prefer \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.

Next: ^ start anchor

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

^ anchor tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful