Regex flags
Flags change how a regex runs: i ignores case, g finds all matches instead of the first, m makes ^ and $ match at line breaks, s lets the dot match newlines, u enables Unicode mode, y matches only at lastIndex (sticky). They go after the closing slash in JavaScript: /abc/gi.
Syntax
/pattern/flags new RegExp("pattern", "gi") Python: re.I, re.M, re.S| Part | What it is |
|---|---|
i | case-insensitive: /cat/i matches CAT |
g | global: find every match, not just the first (match, replace, matchAll) |
m | multiline: ^ and $ match at the start and end of each line |
s | dotAll: the dot also matches line breaks |
u | unicode: enables \p{...} and treats astral characters as one |
y | sticky: match must start exactly at regex.lastIndex |
Examples
"Cat cat CAT".match(/cat/gi)Result: ["Cat", "cat", "CAT"]
"a\nb\nc".match(/^\w$/gm)Result: ["a", "b", "c"]
Each line is matched on its own.
["x1y2".replace(/\d/, "#"), "x1y2".replace(/\d/g, "#")]Result: ["x#y2", "x#y#"]
Without g, replace stops after the first match.
const re = /a/g;
[re.test("aa"), re.test("aa"), re.test("aa")]Result: [true, true, false]
A g regex remembers lastIndex between test calls; reset it or drop the g.
Common mistakes
- Reusing a /g regex with .test in a loop or condition; lastIndex persists, so the third call above returns false.
- Putting flags inside the pattern as (?i); JavaScript supports inline modifiers only in the (?i:...) form since ES2025.
- Forgetting g in replace, so only the first occurrence changes.
- Confusing m and s: m is about ^ and $, s is about the dot.
Practice Regex flags
These drills check your answer on live data, free, no account needed.
- Multiline flag: TODO linesEach test is a block of several lines. Capture the text after TODO: on every line that starts with TODO: (TODO, colon, space).
- Unicode letters with the u flagThe whole line must match. Match a single word made only of letters from any language, such as café or Zoë. Digits, spaces and punctuation are not allowed.
- Anchors: lines that start with ERRORA match anywhere counts, so anchor the pattern yourself. Match lines that begin with the word ERROR in capitals.
Questions
What does the g flag do?
It makes match return every match as an array of strings, replace change every occurrence, and matchAll work at all. Without g, each of these deals with the first match only.
How do I make a regex case-insensitive?
Add the i flag: /hello/i. In Python pass re.I (re.IGNORECASE); in PostgreSQL use the ~* operator; in Excel pass 1 as the case_sensitivity argument.
What is the difference between the m and s flags?
m (multiline) changes the anchors so ^ and $ match at line boundaries. s (dotAll) changes the dot so it matches newlines. They are independent and often used together.
Related
- Anchors ^ and $What ^ and $ match, how they change with the m flag, whole-string versus per-line matching, and the mistakes with alternation and trailing newlines.
- Dot and newlineWhy . does not match line breaks by default, the s flag in JavaScript and re.DOTALL in Python, the portable [\s\S] trick, and how s differs from m.
- Unicode regexHow the u flag changes JavaScript regex, matching any letter with \p{L}, a script with \p{Script=...}, emoji as one character, and ASCII limits of \w and \b.
- match, test, replace in JavaScriptWhich JavaScript regex method to use for a yes/no check, the first match, all matches with groups, substitution and splitting, with return values and g rules.
Previous: Lookbehind (?<=...) and (?<!...)Next: match, test, replace in JavaScript