The g flag (global) tells the regex engine to find every match in a string—not stop after the first hit. It powers replace-all, matchAll(), and exec() loops that walk through all occurrences.
Remember
/pattern/g // enable global
regex.global // true when g is set
regex.lastIndex // search cursor (advances with g)
match() + g → all matched strings
replace() + g → replace every match
exec() + g → next match each call
Use
Prefer
All match strings
str.match(/pat/g)
Replace every hit
str.replace(/pat/g, "x")
Groups for every hit
str.matchAll(/pat/g) or exec loop
Approach
How to Use the g Flag
Add g, then choose the method that fits your goal.
Create a global regex: /pattern/g or new RegExp("pattern", "g").
Use match for a simple list of strings, or replace to change every hit.
Use matchAll or an exec loop when you need capture groups / index for each match.
Before reusing the same global regex on a fresh scan, set re.lastIndex = 0.
re = /pattern/g
re.lastIndex = 0
loop:
m = re.exec(text)
if m is null:
stop
else:
use m[0], m.index, groups…
// lastIndex already moved past this match
Reference
Cheat Sheet
Keep these global-flag facts handy.
Topic
Detail
Syntax
/pattern/g or new RegExp(p, "g")
Check flag
regex.global
match + g
Array of strings (no groups in result)
match without g
One match object (or null)
replace + g
Replace every match
matchAll
Requires global regex; yields full match objects
lastIndex
Reset to 0 before a fresh scan
Standard
ES3+ (universal)
Try it
Live Preview
Compare global vs non-global behavior on "cat dog cat bird cat".
Browser demo of the global flag — same idea as the examples below.
Result
text.match(/cat/g)
→ ["cat", "cat", "cat"]
/* g finds every "cat" */
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — match() With and Without g
Global match returns every string; without g you get one match object.
Advance the cursor. After each hit, lastIndex moves past that match.
Continue until null. When nothing is left, exec returns null and the loop stops.
Reuse carefully. Set re.lastIndex = 0 before scanning the same regex again from the start.
Edge Cases & Pitfalls
Check these before relying on the global flag.
lastIndex
Reset before reusing
A leftover lastIndex can skip matches. Set re.lastIndex = 0 for a fresh scan (see also global).
match
Global match drops groups
With g, match returns strings only. Need captures? Use matchAll or an exec loop.
test
test() also advances lastIndex
Repeated test calls on a global regex can alternate true/false. Prefer a non-global regex for simple yes/no checks.
empty
Zero-length matches
Patterns like /a*/g can match empty strings. Ensure lastIndex advances, or you risk an infinite loop.
Compatibility
Browser & Runtime Support
The g (global) flag is part of core JavaScript regular expression syntax (ES3+).
✓ Baseline · ES3+
RegExp g / global
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
g flagExcellent
Bottom line: Safe everywhere. Use g whenever you need every match — and remember lastIndex when reusing the same RegExp instance.
Analysis
Performance Notes
Approach
Cost
Best for
match(/…/g)
O(n)
Simple list of all match strings
replace(/…/g, …)
O(n)
Replace every occurrence
matchAll / exec loop
O(n)
All matches with groups / index
Global search still walks the string once. Prefer match for plain strings; use matchAll or exec when you need capture details. Avoid restarting from scratch for each hit.
Remember
Key Takeaways
Role:g finds every match, not just the first.
Replace all: always use /pattern/g with replace.
Cursor:lastIndex advances — reset it before reuse.
Groups: prefer matchAll / exec over global match.
One line: add g when you need every match — and respect lastIndex when you reuse the regex.
Frequently Asked Questions
The g flag (global) tells the engine to find all matches in a string, not just the first one. Methods like match(), exec(), test(), 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'). Then regex.global is true.
With g, match() returns an array of matched strings only—no capture groups in that array. Without g, match() returns one match object (with index and groups) or null.
With g, each exec() call advances lastIndex so the next call continues forward. Without g, exec() always starts from the beginning and returns the first match again.
Yes for replace(): string.replace(/pattern/g, replacement) replaces every match. Without g, only the first match changes. replaceAll() also requires a global regex when you pass a RegExp.
The global flag is core JavaScript regex syntax since ES3. It works in every browser and Node.js version.