liveBook · Manning

Global regular expression

As explained in Secrets of the JavaScript Ninja, Third Edition

A regular expression configured to find successive matches throughout its input.

A global regular expression uses the `g` flag to keep finding matches until it reaches the end of the input. Without `g`, a regular expression stops after its first match. section 12.7

Use `matchAll` with a global regular expression to retrieve repeated matches. The `replace` method can transform a string by replacing each match. section 12.7

Set the global flag

The flag can appear in a regex literal, as in `/test/g`, or as the second argument to `new RegExp("test", "g")`. section 12.7

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Defines the g flag, contrasts local and global stopping behavior, and names matching methods without detailing engine internals.
Examples
Shows /test/ig and new RegExp("test", "ig") as ways to specify flags.
What you can do after
Apply it via String.prototype.matchAll to retrieve repeated matches, or replace to transform each match.
How it connects
Connects global regular expressions with regex flags, String.prototype.match, matchAll, and replace.

Also explained in

Compare how 2 books explain global regular expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.