JavaScript RegExp flags Property

Beginner
5 min read
Updated: Nov 2026
3 examples
RegExp

Definition

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"
CallWhat it does
/cat/giflags is "gi".
new RegExp("cat", "mig")flags is sorted to "gim".
regex.flagsRead-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

NameDescription
regex.flagsRead-only string of all flags currently on the RegExp.
Empty stringMeans no flags were set.
OrderThe engine returns flags in a consistent sorted order.

Return Value

ValueDescription
"" (empty)No flags on the RegExp.
Flag stringOne or more of d, g, i, m, s, u, v, y in sorted order.

JavaScript Examples

Example 1 — Read flags

Print the flags string for a few patterns.

JavaScript
console.log(JSON.stringify(/cat/.flags));
console.log(/cat/i.flags);
console.log(/cat/gim.flags);
console.log(new RegExp("cat", "sg").flags);
Try It Yourself

How It Works

  1. No flags → empty string (shown as "").
  2. One flag → that letter.
  3. Several flags → one sorted string.
  4. Constructor flags are normalized the same way.

Example 2 — Check for a flag

Use includes or the boolean properties.

JavaScript
const re = /\d+/gim;

console.log(re.flags.includes("g"));
console.log(re.global);
console.log(re.flags.includes("s"));
console.log(re.dotAll);
Try It Yourself

How It Works

  1. flags.includes("g") is an easy string check.
  2. re.global is the dedicated boolean for g.
  3. Both agree when the flag is present or missing.

Example 3 — Clone a RegExp with extra flags

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"));
Try It Yourself

How It Works

  1. source is the pattern without flags.
  2. Add "g" to the old flags when you need every match.
  3. The new object keeps ignoreCase and gains global.

Frequently Asked Questions

A string of all flags on the RegExp, such as "gi" or "". It is read-only.
The engine sorts flags into a standard order. "mig" becomes "gim".
Use the boolean property (regex.global, regex.ignoreCase, …) or regex.flags.includes("g").
No. Create a new one with new RegExp(old.source, newFlags).
Common ones are d, g, i, m, s, u, v, and y — depending on what you set at creation.
No. toString() returns the full /pattern/flags form. flags returns only the flag letters.

Next: source

Read the pattern text of a RegExp without its flags.

source tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful