JavaScript RegExp global Property

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is global?

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
UsePrefer
Find all matches/pattern/g + loop exec()
First match onlyOmit g
Check the flagregex.global or regex.flags includes "g"

How to Use global

Add g when you create the regex, then read .global or rely on lastIndex.

  1. Create the regex with g: /pattern/g or new RegExp(pattern, "g").
  2. Read regex.global — it should be true.
  3. Use exec() in a loop (or match / matchAll) to get every hit.
  4. Reset regex.lastIndex = 0 before searching the same string again from the start.
PieceRequired?Role
g flagAt creationTurns global search on
regex.globalRead-onlyReports whether g was set
lastIndexWith gWhere 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

Cheat Sheet

Keep these facts handy when using the g flag.

TopicDetail
Propertyregex.global → true / false
Set via/…/g or new RegExp(p, "g")
Writable?No — recreate the RegExp to change flags
With exec / testUpdates lastIndex
Without gAlways search from the start
Related flag pageg modifier

Live Preview

Compare a non-global and global regex on the same string.

Browser demo of RegExp.prototype.global and two exec() calls on "a1 a2 a3".

Result
regex = /\d+/
global → false

exec #1 → "1" (index 1)
exec #2 → "1" (index 1)  /* same first match again */

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Read global

See how the g flag sets the property.

JavaScript
const a = /cat/;
const b = /cat/g;
const c = new RegExp("cat", "gi");

console.log(a.global); // false
console.log(b.global); // true
console.log(c.global); // true
console.log(c.flags);  // "gi"
Try It Yourself

How It Works

  1. No g. /cat/ → global is false.
  2. Literal with g. /cat/g → global is true.
  3. Constructor flags. "gi" sets both global and ignoreCase.
  4. flags string. c.flags reports "gi".

Example 2 — With vs Without g

Call exec twice and compare what you get back.

JavaScript
const str = "a1 a2";
const once = /\d+/;
const all = /\d+/g;

console.log(once.exec(str)[0]); // "1"
console.log(once.exec(str)[0]); // "1"  (repeats)

console.log(all.exec(str)[0]);  // "1"
console.log(all.exec(str)[0]);  // "2"  (advances)
Try It Yourself

How It Works

  1. Non-global. Both exec calls return the first number "1".
  2. Global on. First call returns "1" and moves lastIndex.
  3. Second global call. Continues and finds "2".
  4. Takeaway. Need every match? Use g.

Example 3 — Collect All Matches

Loop exec() while global is true.

JavaScript
const regex = /\d+/g;
const str = "A12 B34 C5";
const found = [];
let match;

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

console.log(found.join(", ")); // "12, 34, 5"
console.log(regex.global);     // true
Try It Yourself

How It Works

  1. Global regex. /\d+/g can walk the whole string.
  2. Loop exec. Each hit is pushed into found.
  3. Stop on null. When no matches remain, the loop ends.
  4. 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.

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 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
global Universal

Bottom line: Use the g flag whenever you need all matches. Remember to manage lastIndex when reusing the same RegExp.

Performance Notes

ApproachCostBest for
Non-global execOne matchFirst hit only
Global exec loopOne step per matchAll matches + groups
str.match(/p/g)All full matchesSimple 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.

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.

Next: [0-9] digits

Learn the character class that matches any digit from 0 to 9.

[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.

8 people found this page helpful