← Back to JavaScript Course | Chapter 9: Async & Web APIs | Lesson 19 of 26

JS RegExp Flags

A regular expression's flags -- single letters placed after the closing slash, like /pattern/gi -- modify how the pattern matches: case-insensitively, globally across every match instead of just the first, or across multiple lines. Understanding each flag is essential for getting the matching behavior you actually intend.

The Global Flag (g)

Without g, a regex only finds the first match in a string and stops -- with g, methods like .match() and .replace() find and act on every match throughout the entire string, not just the first occurrence.

Note: Always include g with .replace() when you intend to replace every occurrence, not just the first one found.

Warning: A regex with the g flag becomes stateful when reused with .test() or .exec() -- it remembers its position between calls via lastIndex, which can produce surprising alternating results.

Example: The Global Flag (g)

javascript
console.log("cat cat cat".match(/cat/));   // only the first match
console.log("cat cat cat".match(/cat/g));  // every match

The Case-Insensitive Flag (i)

The i flag makes a pattern match regardless of upper or lower case -- /cat/i matches "cat", "CAT", "Cat", and any other capitalization -- useful whenever the exact case of the input cannot be relied upon, like validating a user-entered word.

Note: Use i specifically when case genuinely should not matter for the match, rather than adding it as a reflexive default to every pattern.

Warning: Adding i to a pattern that specifically needs to distinguish case (like matching a case-sensitive code or identifier) will incorrectly widen the match to unintended cases.

Example: The Case-Insensitive Flag (i)

javascript
console.log(/cat/i.test("CAT")); // true, case-insensitive

The Multiline Flag (m)

By default, ^ matches only the very start of the entire string and $ only the very end, even in a multi-line string -- the m flag changes this so ^ and $ instead match the start and end of each individual line within the string.

Note: Use m specifically when working with a multi-line string where you need to match patterns at the start or end of each line, not just the whole string.

Warning: Forgetting the m flag when working with multi-line text is a common reason ^ or $ anchors appear to "not work" as expected on anything but the very first or last line.

Example: The Multiline Flag (m)

javascript
const text = "line one\nline two";
console.log(text.match(/^line/gm)); // matches start of each line

The Unicode Flag (u)

The u flag enables full Unicode-aware pattern matching, correctly handling characters outside the Basic Multilingual Plane (like many emoji), which without this flag can be misinterpreted as two separate characters instead of one.

Note: Include u when a pattern needs to correctly match emoji or other characters represented by surrogate pairs in UTF-16.

Warning: Without u, a pattern like /./ can match only half of an emoji character, since JavaScript strings internally use UTF-16 and some characters span two code units.

Example: The Unicode Flag (u)

javascript
console.log(/\u{1F600}/u.test("\u{1F600}")); // correctly handles characters outside the basic plane

Combining Multiple Flags

Flags can be combined in any order after the closing slash -- /pattern/gi applies both global and case-insensitive matching at once, /pattern/gm combines global and multiline, and so on, letting you mix exactly the behaviors a given task needs.

Note: Combine only the flags a specific pattern actually needs -- adding unnecessary flags does not usually break anything, but can make the intended matching behavior less clear to a reader.

Warning: The order flags are written in does not matter (gi and ig behave identically), but each flag can only appear once -- repeating a flag causes a syntax error.

Example: Combining Multiple Flags

javascript
console.log("Cat cat CAT".match(/cat/gi)); // combines global + case-insensitive
Common Mistakes
  1. Forgetting the g flag on a pattern used with .replace(), which then only replaces the very first match instead of every occurrence in the string.
  2. Reusing a regex object with the g flag across multiple .test() calls without resetting lastIndex, causing alternating true/false results due to the stateful matching position.
  3. Adding the i flag out of habit even when case sensitivity is actually the desired, intentional behavior.
Chapter Summary
  • g (global) finds all matches in a string, not just the first.
  • i (case-insensitive) matches regardless of upper or lower case.
  • m (multiline) makes ^ and $ match the start/end of each line rather than only the whole string.
Browser Support

All standard regex flags (g, i, m, s, u, y) are supported in every modern browser.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.