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
Fundamentals
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.
Concept
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.
Foundation
📝 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.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Check global flag
regex.global
Global literal
/\\d+/g
All matches via match
str.match(/pat/g)
Loop with exec
while ((m = /pat/g.exec(s)) !== null)
Reset scan position
regex.lastIndex = 0
See all flags
regex.flags
Compare
📋 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
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. Examples highlight how global changes matching behavior.
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.
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
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.
Applications
🚀 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.
Important
📝 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.
Compatibility
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 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
RegExp.globalExcellent
Bottom line: Safe everywhere. Always pair global regexes with lastIndex awareness when reusing the same object.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about global
The g flag and finding every match.
5
Core concepts
📝01
Property
regex.global
API
🌐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.