The g modifier (global flag) tells the regex engine to find every match in a string—not stop after the first hit. It powers replace-all workflows, matchAll(), and exec() loops that walk through all occurrences.
01
g flag
Find all
02
global
RegExp prop
03
lastIndex
Advances
04
match()
All strings
05
replace
/pat/g
06
matchAll
Needs g
Fundamentals
Introduction
By default, many regex operations stop after the first successful match. Add the g modifier when you need to process all occurrences—counting words, replacing every match, or collecting every date in a log file.
The global flag changes how exec(), test(), match(), and replace() behave. It also sets the global property on the RegExp object to true, which you can inspect at runtime.
Concept
Understanding the g Modifier
Append g to a regex literal or include "g" in constructor flags. With global enabled, the engine tracks position via lastIndex and continues searching after each match.
JavaScript
const text = "cat dog cat";
/cat/.exec(text); // ["cat"] — first only
/cat/g.exec(text); // ["cat"], lastIndex → 3
text.match(/cat/g); // ["cat", "cat"] — all matches
Without g, repeated exec() calls on the same regex keep returning the first match. With g, each call moves forward until no matches remain.
💡
Beginner Tip
To replace every occurrence, always use the global flag: text.replace(/old/g, "new"). A single replace without g changes only the first match.
Usage
How to Use the g Modifier
Enable global search on literals, constructors, and combined flags:
JavaScript
/hello/g; // global literal
new RegExp("hello", "g"); // constructor
/pattern/gi; // global + ignoreCase
re.global; // true when g is set
text.matchAll(/\\d+/g); // requires g (or y with lookahead)
text.replace(/cat/g, "dog"); // replace all
Combine g with other flags like i (case-insensitive) or d (indices). Flag order in the literal does not matter: /cat/gi equals /cat/ig.
Foundation
📝 Syntax
JavaScript
/pattern/g // global: all matches
/pattern/gi // global + ignoreCase
/pattern/gd // global + indices
new RegExp("p", "g") // constructor
regex.global // true | false
regex.lastIndex // search cursor (with g)
Common patterns
/pattern/g with match() — array of all matched strings.
while ((m = re.exec(s)) !== null) — loop all matches with capture groups.
replace(/pat/g, "new") — replace every occurrence.
matchAll(/pat/g) — iterator of full match objects.
Cheat Sheet
⚡ Quick Reference
Goal
Pattern / API
Enable global
/pattern/g
Check flag
regex.global
All match strings
str.match(/pat/g)
Loop with groups
exec() + g
Replace all
str.replace(/pat/g, "x")
Iterator of matches
str.matchAll(/pat/g)
Compare
📋 With g vs without g
How the same pattern behaves with and without the global flag.
No g
/cat/
First match only
With g
/cat/g
Every match
match() + g
["cat","cat"]
Strings only
match() no g
["cat"] + index
One result + groups
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. Examples cover match(), exec loops, replace-all, matchAll, and the global property.
📚 Getting Started
Collect every match with String.match().
Example 1 — match() With and Without g
Global match returns all strings; non-global returns one result.
JavaScript
const text = "cat dog cat bird cat";
console.log(text.match(/cat/g));
// ["cat", "cat", "cat"]
console.log(text.match(/cat/));
// ["cat"] — first only, includes index input
The global flag is required for replace-all with replace(). Alternatively, replaceAll() accepts a string or a global regex.
Example 4 — Iterate with matchAll()
Get full match objects (with capture groups) for every date in text.
JavaScript
const text = "2024-01-15 and 2025-07-17";
const re = /(\d{4})-(\d{2})-(\d{2})/g;
for (const m of text.matchAll(re)) {
console.log(m[0], "year=" + m[1]);
}
// "2024-01-15 year=2024"
// "2025-07-17 year=2025"
The g global modifier is part of core JavaScript regular expression syntax.
✓ Baseline · ES3+
RegExp g flag
Supported in every browser and Node.js version. The global flag enables all-match search and replace behavior.
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
gExcellent
Bottom line: Safe everywhere. Use g whenever you need every match, not just the first one.
Wrap Up
Conclusion
The g modifier enables global matching—finding, replacing, or iterating every occurrence in a string. It sets global: true, advances lastIndex, and changes what match() returns.
Use /pattern/g for replace-all and matchAll(), loop with exec() when you need capture groups, and reset lastIndex when reusing regex objects. Next, learn the i ignoreCase modifier.
Use global regex when only the first match matters
Assume you can add g to an existing RegExp object
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about the RegExp g modifier
Find and replace every match in a string, not just the first.
5
Core concepts
🔄01
g flag
Global.
Syntax
📈02
lastIndex
Cursor.
State
🚫03
match()
All strings.
API
🔒04
/pat/g
Replace all.
Replace
🔢05
matchAll
Iterator.
Groups
❓ Frequently Asked Questions
The g flag (global) tells the engine to find all matches in a string, not just the first one. Methods like exec(), test(), match(), matchAll(), and replace() behave differently when global is enabled.
Append g to the literal: /pattern/g, combine flags like /pattern/gi, or pass 'g' in new RegExp('pattern', 'g'). The global property on the instance becomes true.
With g, match() returns an array of matched strings only—no capture groups in the array. Without g, match() returns one match object (with index and groups) or null.
Without the global flag, exec() always starts from the beginning and returns the first match again. With g, each exec() call advances lastIndex to continue searching.
Yes. string.replace(/pattern/g, replacement) replaces every match. Without g, only the first match is replaced. You can also use string.replaceAll() for string or global-regex patterns.
The global flag is core JavaScript regex syntax since ES3. It works in every browser and Node.js version.
Did you know?
Before matchAll() (ES2020), developers used a while loop with exec() and the g flag to collect every match with capture groups. matchAll() wraps that pattern in a clean iterator—but it still requires the global flag.