regex.global is a read-only boolean. It is true when the RegExp was created with the g flag — so searches can find all matches, not only the first.
Remember
/cat/.global → false
/cat/g.global → true
with g:
exec() / test() advance lastIndex
can find every match in a loop
without g:
exec() always returns the first match again
flags are fixed at creation — cannot set .global = true
Add g when you create the regex, then read .global or rely on lastIndex.
Create the regex with g: /pattern/g or new RegExp(pattern, "g").
Read regex.global — it should be true.
Use exec() in a loop (or match / matchAll) to get every hit.
Reset regex.lastIndex = 0 before searching the same string again from the start.
Piece
Required?
Role
g flag
At creation
Turns global search on
regex.global
Read-only
Reports whether g was set
lastIndex
With g
Where the next search starts
Pseudocode
Pseudocode
regex = /pattern/g // global = true
str = text to search
while (match = regex.exec(str)) is not null:
use match
// lastIndex moved forward automatically
// restart from the beginning:
regex.lastIndex = 0
Stop on null. When no matches remain, the loop ends.
Confirm flag.regex.global is still true.
Edge Cases & Pitfalls
Check these before reusing a global RegExp.
lastIndex
Stale lastIndex
After a partial search, the next exec/test may start mid-string. Reset with regex.lastIndex = 0.
readonly
Cannot assign regex.global = true
Create a new RegExp with the g flag instead.
trap
Mixing test and exec
Both update lastIndex when global is true. Reset between different kinds of searches on the same object.
empty
Zero-length matches
Some patterns can match empty strings and stall a loop. Advance lastIndex when a match has length 0.
Compatibility
Browser & Runtime Support
RegExp.prototype.global and the g flag are core language features — available in every JavaScript environment.
✓ Baseline · Widely available
RegExp.prototype.global
Supported in all browsers and Node.js versions. Safe for every target with no polyfill.
100%Universal support
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
globalUniversal
Bottom line: Use the g flag whenever you need all matches. Remember to manage lastIndex when reusing the same RegExp.
Analysis
Performance Notes
Approach
Cost
Best for
Non-global exec
One match
First hit only
Global exec loop
One step per match
All matches + groups
str.match(/p/g)
All full matches
Simple list of match strings
Global search walks the string once across the loop. Prefer a non-global regex when you only need the first match — clearer and no lastIndex state.
Remember
Key Takeaways
Role:global is true when the g flag was set at creation.
Effect:exec/test advance lastIndex to find every match.
Readonly: recreate the RegExp to change flags — do not assign .global.
Reset: set lastIndex = 0 before re-scanning from the start.
One line: the g flag makes global true so you can walk every match — manage lastIndex when you reuse the regex.
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 searches can find all matches — 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.
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 from the start.
Without g, exec() always starts from the beginning and returns the first match again. With global true, each exec() call advances via lastIndex.
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.
It returns an array of all full match strings. Without g, match() returns one match result (with groups) or null.