JavaScript RegExp global Property

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
g flag · all matches

What You’ll Learn

The global property is true when a RegExp was built with the g flag. That tells JavaScript to find every match in a string—not stop after the first hit—and to advance lastIndex on repeated exec() calls.

01

Property

regex.global

02

g flag

/pat/g

03

Boolean

true / false

04

lastIndex

Advances

05

exec loop

All matches

06

Read-only

Fixed flags

Introduction

By default, a regular expression finds the first place a pattern fits in a string. The global (g) flag changes that behavior: the engine keeps searching for additional matches after the first.

The global property simply reports whether that flag is active. You set it at creation time with /pattern/g or new RegExp("pattern", "g"), then read regex.global to branch logic or debug.

Understanding the global Property

RegExp.prototype.global is a read-only boolean:

  • true — the g flag is set; repeated exec() / test() calls advance through the string.
  • false — only the first match is considered; each exec() on the same string returns the same first result.
  • Reflects flags — also visible in regex.flags when it contains "g".
  • Immutable — you cannot flip global after construction; create a new RegExp instead.
  • Pairs with lastIndex — global regexes track where the next search should start.
💡
Beginner Tip

If your while ((m = regex.exec(str)) !== null) loop never ends or repeats the same match, check regex.global. Without g, the loop will not advance.

📝 Syntax

JavaScript
regex.global

How to set global mode (at creation)

JavaScript
/pattern/g
new RegExp("pattern", "g")
new RegExp("pattern", "gi")   // global + ignoreCase

Return value

  • true if the g flag is present.
  • false otherwise.

⚡ Quick Reference

GoalCode
Check global flagregex.global
Global literal/\\d+/g
All matches via matchstr.match(/pat/g)
Loop with execwhile ((m = /pat/g.exec(s)) !== null)
Reset scan positionregex.lastIndex = 0
See all flagsregex.flags

📋 Global vs Non-Global RegExp

The same pattern behaves differently depending on the g flag.

global: false
first match

Default

global: true
all matches

g flag

exec loop
needs g

Advance

match()
array vs object

Return shape

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Examples highlight how global changes matching behavior.

📚 Getting Started

See global true vs false on two regexes.

Example 1 — Read the global Property

Compare a regex with and without the g flag.

JavaScript
const withGlobal = /cat/g;
const withoutGlobal = /cat/;

console.log(withGlobal.global);     // true
console.log(withoutGlobal.global);  // false
Try It Yourself

How It Works

The trailing g in the literal sets global to true. Without it, the engine stops after the first successful match.

📈 Practical Patterns

Looping, match(), flags, and reuse.

Example 2 — Loop All Matches with exec()

A global regex advances through the string on each call.

JavaScript
const regex = /\d+/g;
const text = "a1 b22 c333";
const found = [];

let match;
while ((match = regex.exec(text)) !== null) {
  found.push(match[0]);
}

console.log(regex.global);  // true
console.log(found);         // ["1", "22", "333"]
Try It Yourself

How It Works

Because global is true, each exec() updates lastIndex and finds the next digit sequence until none remain.

Example 3 — String.match() with Global

Global match() returns every full match; non-global returns one result with groups.

JavaScript
const text = "red green red";

console.log(text.match(/red/g));  // ["red", "red"]
console.log(text.match(/red/));   // ["red"] (with index input groups)
Try It Yourself

How It Works

When regex.global is true, match() collects all full matches into a flat array. Capture groups are omitted from that array—use exec() in a loop when you need groups for every match.

Example 4 — Compare global with flags

The flags string is another way to inspect active modes.

JavaScript
const regex = new RegExp("hello", "gi");

console.log(regex.global);           // true
console.log(regex.flags);            // "gi"
console.log(regex.flags.includes("g"));  // true
Try It Yourself

How It Works

global is a convenient boolean for the g slice of flags. Other properties like ignoreCase and multiline mirror their flag letters the same way.

Example 5 — Reset lastIndex Before Reuse

Global regexes remember position—reset when scanning again.

JavaScript
const regex = /a/g;
const text = "a a a";

regex.exec(text);  // first "a", lastIndex moves
regex.exec(text);  // second "a"

console.log(regex.lastIndex);  // 3

regex.lastIndex = 0;
console.log(regex.exec(text)[0]);  // "a" again from start
Try It Yourself

How It Works

Shared global regexes in modules or functions can surprise you with a non-zero lastIndex. Set it to 0 (or create a fresh RegExp) before a new full-string scan.

🚀 Common Use Cases

  • Find all tokens — extract every number, word, or tag in a document.
  • Replace all — pair with replaceAll or global replace.
  • Validate repeated parts — ensure every comma-separated segment matches a pattern.
  • Count occurrences — loop exec() and increment a counter.
  • Branch logic — if regex.global, use a loop; otherwise read one match.
  • matchAll iterators — modern API requires global (or sticky) regexes.

🧠 How Global Matching Advances

1

Create with g

global becomes true; lastIndex starts at 0.

/pat/g
2

First exec

Engine finds the first match and sets lastIndex past it.

exec()
3

Next exec

Search resumes at lastIndex until no match returns null.

Loop
4

Reset or discard

Set lastIndex = 0 to scan again, or use a new RegExp.

📝 Notes

  • global is read-only—flags are fixed at creation.
  • Without g, an exec() loop returns the same first match forever.
  • Global match() drops capture groups from its array result.
  • lastIndex can cause bugs when the same global regex is reused.
  • regex.flags lists all flags; regex.global is the boolean for g alone.
  • String.prototype.matchAll requires a global (or sticky) RegExp.

Browser & Runtime Support

The global property on RegExp instances is part of standard JavaScript and is supported in all modern environments.

Baseline · ES3+

RegExp global

Supported everywhere RegExp is available. The flags string (ES6) and matchAll (ES2020) extend global matching on modern runtimes.

99% Universal API
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
RegExp.global Excellent

Bottom line: Safe everywhere. Always pair global regexes with lastIndex awareness when reusing the same object.

Conclusion

The global property tells you whether a RegExp searches for all matches. Turn it on with the g flag, loop with exec() or use match(), and reset lastIndex when reusing the same object.

Next, learn about the [0-9] character class group pattern, or review exec() for capture-group details.

💡 Best Practices

✅ Do

  • Add g when you need every match
  • Reset lastIndex before re-scanning
  • Use exec() loops when you need groups for each match
  • Check regex.global before writing a loop
  • Prefer matchAll for modern global iteration

❌ Don’t

  • Loop exec() without the g flag
  • Share one global regex across unrelated scans without resetting
  • Try to toggle global after creation
  • Expect capture groups in global match() results
  • Forget that empty matches can affect lastIndex

Key Takeaways

Knowledge Unlocked

Five things to remember about global

The g flag and finding every match.

5
Core concepts
🌐 02

g flag

/pat/g

Set
🔄 03

lastIndex

Advances.

State
🔍 04

exec loop

All hits.

Pattern
📋 05

Read-only

New RegExp.

Flags

❓ Frequently Asked Questions

A read-only boolean on a RegExp instance. It is true when the pattern was created with the g (global) flag, meaning the engine can search for all matches in a string—not just the first one.
Use a literal with g: /pattern/g, or the constructor: new RegExp('pattern', 'g'). The global property reflects that flag at creation time.
Without g, exec() always starts from the beginning and returns the first match again. Set global to true so each exec() call advances via lastIndex.
When global is true, exec() and test() update lastIndex after each match so the next call continues searching. Reset lastIndex to 0 before re-scanning the same string.
It returns an array of all full matches (strings only—no capture groups in the array). Without g, match() returns one match object with groups.
No. Flags are fixed when the object is created. To toggle global behavior, build a new RegExp with or without g in the flags string.
Did you know?

ES2020 added String.prototype.matchAll, which requires a global RegExp and yields an iterator of match objects with capture groups—a modern alternative to manual exec() loops.

Continue to Group [0-9]

Learn the digit character class for matching numbers in patterns.

Group [0-9] 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