Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 38 ยท Flags and Engines

Regex Flags in Depth

Learn what each regular expression flag does, from global and case-insensitive matching to multiline, dotAll, unicode, sticky and indices, with runnable JavaScript examples.

A flag (also called a modifier) changes how the whole pattern behaves without changing the pattern text. The same /a.c/ matches different strings depending on whether you add i, s or u. Flags are tiny, but forgetting one is behind a surprising share of regex bugs: a replace that only changes the first match, a ^ that refuses to match at the start of a line, or a dot that stops at a newline.

This lesson walks through every flag in JavaScript's regex engine, which runs everywhere you have a browser or Node.js. Other engines spell flags differently (some use inline (?i) syntax), and you will see those differences in regex flavors and engines. The ideas carry over directly.

Where flags go

In a literal, flags follow the closing slash: /cat/gi. With the constructor, they are the second argument: new RegExp("cat", "gi"). The flags property returns them in a canonical order, and each flag also has a boolean property such as .global or .ignoreCase.

Node.js Reading flags from a regex

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

g: global

Without g, String.prototype.match and replace stop at the first match. With g, they find all of them. This is the flag you will use most.

Node.js The g flag with match and replace

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

A regex with g is stateful. It remembers where the last match ended in its lastIndex property, and test() and exec() start from there. Reusing one global regex object across different strings is a classic bug:

JavaScript
const re = /a/g;
re.test("a");   // true,  lastIndex is now 1
re.test("a");   // false, search started at index 1; lastIndex resets to 0

Avoid g when you only call test(). If you must reuse a global regex, set re.lastIndex = 0 first, or create a fresh one.

i: ignore case

i makes letters match regardless of case. By default JavaScript uses simple case folding, which handles ordinary Latin, Greek and Cyrillic letters. Combine it with u or v for full Unicode folding rules.

JavaScript
/hello/i.test("HeLLo");        // true
/[a-z]+/i.exec("ABC def")[0];  // "ABC"

m: multiline

By default ^ matches only at the very start of the string and $ only at the very end. With m, they also match at the start and end of every line (after and before \n, \r, and the Unicode line separators).

Node.js Anchors with and without m

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The name is misleading: m does not make the pattern span multiple lines. It only changes what the anchors mean.

s: dotAll

The dot normally matches any character except line terminators. The s flag lets it match those too, which is the easy way to match across lines.

Node.js dotAll and the newline problem

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

u and v: Unicode modes

Without u, a JavaScript regex sees strings as UTF-16 code units, so an emoji outside the basic plane looks like two characters. u switches to code points, enables \u{1F600} escapes and \p{...} property escapes, and makes invalid escapes a syntax error. v is the newer superset that adds set operations and string properties inside classes. Both get their own lesson: Unicode and the v flag.

JavaScript
"๐Ÿ˜€".match(/./g).length;    // 2 (two surrogate halves)
"๐Ÿ˜€".match(/./gu).length;   // 1

y: sticky

A sticky regex matches only at lastIndex, never later. That makes it ideal for hand-written lexers, covered in sticky matching and match indices.

d: indices

With d, a match carries an indices array holding the start and end of every capture group. You will use it for highlighting and editors, also covered in the next lesson.

All flags at a glance

Flag Property Effect
g global Find all matches; makes the regex stateful
i ignoreCase Case-insensitive matching
m multiline ^ and $ match at line boundaries
s dotAll . also matches line terminators
u unicode Code points, \p{}, strict syntax
v unicodeSets Superset of u with class set operations
y sticky Match only at lastIndex
d hasIndices Record start and end of each group

u and v cannot be used together. Order in the literal does not matter.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does "a-b-c".replace(/-/, "+") return?

    Without the g flag only the first match is replaced.
  2. 2. What does the m flag change?

    The multiline flag only affects the anchors. Spanning lines is done with s or a class such as [\s\S].
  3. 3. Why can re.test(str) return different answers on repeated calls when re = /a/g?

    A global regex starts each test or exec at lastIndex and updates it, so a second call can begin after the previous match.
  4. 4. Which flag lets . match a newline?

    The s flag is called dotAll because the dot then matches every character.

Summary

  • Flags change the matching rules of the entire pattern: g, i, m, s, u, v, y, d.
  • g finds all matches but makes test and exec stateful through lastIndex.
  • m affects only ^ and $; s affects only the dot.
  • u and v switch to Unicode code points; y anchors each attempt at lastIndex; d records group positions.
  • RegExp.prototype.flags and the boolean properties let you inspect the active flags.

Next: Unicode and the v Flag