Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference Β· RegExp

Regular expression flags

Reference for JavaScript regular expression flags: what g, i, m, s, u, v, y and d do, how they combine, and how to read them from the flags property.

Flags are single letters after the closing slash of a regex literal (/pattern/gi) or in the second argument of new RegExp(). They change how the pattern is matched: whether it finds every match, ignores case, treats lines separately, understands Unicode, and so on.

There are eight flags. Order does not matter; reading regex.flags always lists the ones that are set in a fixed order, dgimsuvy. This is a concept page: each flag is explained below with live examples.

JavaScript
const regex = /hello/gi;
regex.flags;      // "gi"
regex.global;     // true
regex.ignoreCase; // true

Try it

JS g, i, m and s

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS u, v, y and d

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The flags

Flag Property Meaning
d hasIndices Adds an indices array with the start and end of each group to match results.
g global Finds all matches. Makes exec() and test() stateful through lastIndex. Required by replaceAll() and matchAll().
i ignoreCase Ignores differences in letter case.
m multiline ^ and $ match at line starts and ends, not only at the string's ends.
s dotAll . also matches line terminators.
u unicode Treats the pattern and text as code points, and enables \u{...} and \p{...}. Stricter syntax.
v unicodeSets An upgrade of u with set subtraction (--), intersection (&&) and string properties. Cannot be combined with u.
y sticky Matches only at the position in lastIndex, never later.

Usage notes

  • g changes how methods behave. match() with g returns all matched strings (without groups), and replace() with g replaces every match. matchAll() and replaceAll() throw a TypeError if the regex lacks g.
  • Use u or v for emoji and non-Latin text, or . will match half a surrogate pair. Prefer v in new code where supported.
  • Without s, . skips \n, \r, 
 and 
. A common workaround is [\s\S].
  • m does not change .. It affects only the anchors ^ and $.
  • i with u uses full Unicode case folding, so a few more characters match each other.
  • Unknown or repeated flags throw a SyntaxError when the regex is created, and u with v is an error.
  • Flags are fixed. The properties are read-only; to change flags make a new regex with new RegExp(regex, "newflags").
  • Sticky vs anchored: y anchors at lastIndex, ^ at the start of the string (or line). They are different tools.

Browser support

g, i, m and y and u are supported everywhere modern. s and d are newer and the v flag is the most recent, so check a compatibility table; the v example above uses a try/catch for that reason.