Regex Flags Explained — g, i, m, s, u, y in JavaScript

Complete guide to JavaScript regex flags: global (g), case-insensitive (i), multiline (m), dotall (s), unicode (u), and sticky (y). When and how to use each flag.

Flags

Detailed Explanation

JavaScript Regex Flags

Flags modify how the regex engine processes your pattern. JavaScript supports six flags, each controlling a different aspect of matching behavior.

Flag Reference

Flag Name Effect
g Global Find all matches, not just the first
i Case-insensitive Ignore case when matching
m Multiline ^ and $ match line boundaries
s Dotall . matches newline characters
u Unicode Enable full Unicode support
y Sticky Match only at lastIndex position

Global Flag (g)

Without g, the regex stops after the first match. With g, it finds all matches:

"banana".match(/a/g)  // ["a", "a", "a"]
"banana".match(/a/)   // ["a"] (only first)

Case-Insensitive Flag (i)

Makes the entire pattern case-insensitive:

/hello/i.test("Hello")  // true
/hello/i.test("HELLO")  // true

Multiline Flag (m)

Changes ^ and $ to match line boundaries instead of string boundaries:

"line1\nline2".match(/^line/gm)  // ["line", "line"]
"line1\nline2".match(/^line/g)   // ["line"] (first only)

Dotall Flag (s)

Makes . match newline characters (\n, \r):

/a.b/s.test("a\nb")   // true
/a.b/.test("a\nb")    // false

Unicode Flag (u)

Enables full Unicode support, including surrogate pairs and Unicode property escapes:

/\u{1F600}/u.test("\u{1F600}")  // true (emoji)
/\p{Letter}/u.test("\u{00E9}")     // true (accented letter)

Sticky Flag (y)

Matches only at the position indicated by lastIndex:

const re = /\d+/y;
re.lastIndex = 4;
re.exec("abc 123")  // matches "123" starting at index 4

Combining Flags

Flags can be combined: /pattern/gims. The most common combinations are gi (global, case-insensitive) and gm (global, multiline).

Use Case

You need to control how your regex engine processes patterns, such as finding all matches in a string, ignoring case differences, processing multi-line text, or working with Unicode characters like emoji.

Try It — Regex Cheat Sheet

Open full tool