JS RegExp Flags
In this page:
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)
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)
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)
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)
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
console.log("Cat cat CAT".match(/cat/gi)); // combines global + case-insensitive
- 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.
- 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.
- Adding the i flag out of habit even when case sensitivity is actually the desired, intentional behavior.
- 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.
All standard regex flags (g, i, m, s, u, y) are supported in every modern browser.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS JSON
- JS Regular Expressions
- JS Fetch API
- JS LocalStorage and SessionStorage
- JS Cookies
- JS setTimeout and setInterval
- JS Event Loop
- JS Web Workers
- JS Service Workers
- JS AJAX
- JS AJAX Intro
- JS AJAX XMLHttp
- JS AJAX Request
- JS AJAX Response
- JS AJAX XML
- JS AJAX PHP
- JS AJAX Database
- JS JSONP
- JS RegExp Flags
- JS RegExp Classes
- JS RegExp Metachars
- JS RegExp Assertions
- JS RegExp Groups
- JS RegExp Quantifiers
- JS JSON HTML
- JS JSON vs XML