RegExp.prototype.flags
Returns the regex's flags as a single string in canonical order.
get RegExp.prototype.flags- Added in JavaScript ES2015 (ES6)
Return value
stringA string of the flags in the canonical order d, g, i, m, s, u, v, y. An empty string when the regex has no flags.
Errors
TypeError if the receiver is not an object.
Changelog
- PHP ES2018s (dotAll) joined the string; d (hasIndices) was added in ES2022 and v (unicodeSets) in ES2024, each inserted at its alphabetical position.
- PHP ES2015The getter reads each individual flag accessor in turn, so a subclass overriding global or ignoreCase changes what flags reports.
flags gives you every modifier on a regex as one string, always in the canonical order dgimsuvy regardless of how you typed them. Together with source it is everything you need to clone, serialise or modify a regex.
Read-only
There is no setter. Assigning to re.flags throws a TypeError in strict mode — which includes all module code and class bodies — and silently does nothing in sloppy mode. Flags are fixed at construction, so changing one means building a new regex: new RegExp(re.source, re.flags + "g").
Normalisation cuts both ways
Because the order is canonical, comparing a.flags === b.flags is a reliable set comparison — /x/ig and /x/gi both report "gi". But it also means the string you get back is not the one you passed in, which surprises snapshot tests that assert on the constructor argument.
Adding a flag naively can produce a duplicate and throw at construction, so check flags.includes("g") first.
Example
When to use it
- Clone a regex and add the g flag before passing it to matchAll
- Serialise a regex to JSON as a source and flags pair
- Detect whether a caller-supplied regex is global before iterating with it
Frequently asked questions
Can I change a regex's flags after creating it?
No. flags and each individual flag property are getters with no setter, so assignment throws in strict mode. Create a new regex instead: new RegExp(re.source, newFlags).
What order does flags return the letters in?
Always d, g, i, m, s, u, v, y — alphabetical — no matter what order you wrote them. That is why /x/ig.flags and /x/gi.flags both equal "gi".
How do I check whether a regex has the global flag?
Read re.global for a direct boolean, or re.flags.includes("g") if you are already working with the flag string. Both are equivalent; the dedicated property is clearer at a call site.
More examples
Adding and removing a flag safely
The includes() check matters: passing "ig" + "i" to the constructor throws a SyntaxError for the repeated flag. Note the result reads "gi" even though "ig" was assembled — the getter normalises the order.
const withFlag = (re, f) =>
new RegExp(re.source, re.flags.includes(f) ? re.flags : re.flags + f);
const withoutFlag = (re, f) =>
new RegExp(re.source, re.flags.replace(f, ""));
const base = /cat/i;
console.log(withFlag(base, "g").flags); // "gi"
console.log(withFlag(withFlag(base, "g"), "g").flags); // "gi" - no dup
console.log(withoutFlag(withFlag(base, "g"), "i").flags); // "g"Order is normalised, not preserved
Whatever order you write the flags in, the getter rebuilds the string in the fixed order d, g, i, m, s, u, v, y. That makes === a correct way to ask whether two regexes carry the same flags.
console.log(new RegExp("a", "misgudy").flags); // "dgimsuy"
console.log([...new RegExp("a", "yg").flags]); // [ "g", "y" ]
// So set comparison is just string equality:
console.log(/x/ig.flags === /x/gi.flags); // true
Discussion