JS: Regex Flag g (global)

By Xah Lee. Date: .

Flag g (global)

Find all occurrences.

Used by some regex functions. e.g.

// demo regex flag g

// with flag g. get all occurrences
console.log(
 "something".match(/[aeiou]/g),
);
// [ "o", "e", "i" ]

// without flag g.
console.log(
 "something".match(/[aeiou]/),
);
// [ "o", index: 1, input: "something", groups: undefined ]

Property name regexp.prototype.global

the property RegExp.prototype.global is read-only.

it allows you to know if a flag is set for a given regex object.

// RegExp.prototype.global
const regexobj = /([aeiou])/g;
console.assert(
 regexobj.global === true,
);