In JavaScript, regex.flags is a read-only string. It lists every flag on the RegExp in a standard order.
Use it when you need a quick summary of g, i, m, s, u, y, d, and v without checking each boolean property one by one.
Syntax
Create the regex with flags, then read .flags.
JavaScript
const a = /cat/;
const b = /cat/gi;
const c = new RegExp("cat", "mig");
console.log(a.flags); // ""
console.log(b.flags); // "gi"
console.log(c.flags); // "gim"
Call
What it does
/cat/gi
flags is "gi".
new RegExp("cat", "mig")
flags is sorted to "gim".
regex.flags
Read-only string of all active flags.
Remember this at a glance:
Remember
/cat/.flags → ""
/cat/gi.flags → "gi"
/cat/ims.flags → "ims"
flags is a string, not a boolean
order is sorted by the engine (not your typing order)
cannot assign regex.flags = "g"
What You Get
Name
Description
regex.flags
Read-only string of all flags currently on the RegExp.
Empty string
Means no flags were set.
Order
The engine returns flags in a consistent sorted order.
Return Value
Value
Description
"" (empty)
No flags on the RegExp.
Flag string
One or more of d, g, i, m, s, u, v, y in sorted order.
Build a new regex from source and a new flags string.
JavaScript
const base = /hello/i;
const all = new RegExp(base.source, base.flags + "g");
console.log(base.flags);
console.log(all.flags);
console.log(all.test("HELLO"));