Guide
Regex Flags Explained: What g, i, m, s, and u Actually Do
Understand what regex flags change in practice, including global matching, case-insensitive matching, multiline anchors, dotAll behavior, and Unicode handling.
Published
Updated
Quick answer
gfinds all matches instead of stopping after the first one.iignores case differences.mmakes^and$work per line instead of only at the start and end of the whole string.slets.match newline characters.uenables more correct Unicode-aware behavior.
What regex flags do
A regex pattern does not run in isolation. Flags, also called modifiers, change how the engine interprets that pattern. That is why the same expression can return one match, many matches, or no matches at all depending on which flags are enabled.
If you debug regexes in JavaScript, TypeScript, or browser tooling, these flags are often the first thing to check when a pattern behaves unexpectedly.
The global flag: g
The global flag tells the engine to keep scanning for every match instead of stopping after the first one. That matters when you are extracting repeated values from logs, URLs, or free-form text.
Without g, a pattern like \d+ against Order 12, 45, 900 returns only the first number. With g, it can return all three matches.
The case-insensitive flag: i
The i flag makes letter matching ignore uppercase versus lowercase differences. That is useful for user input, HTTP headers, and loosely formatted text where casing is inconsistent.
A pattern like error will miss ERROR and Error unless you add i. This is one of the most common reasons a regex looks correct but returns no matches.
The multiline flag: m
The multiline flag changes how the anchors ^ and $ work. Without m, they only refer to the start and end of the entire string. With m, they also match the start and end of each line.
That makes ^ERROR useful for scanning multi-line logs line by line instead of only checking whether the entire text begins with ERROR.
The dotAll flag: s
Normally the dot character . does not match newline characters. The s flag changes that so a dot can span across line breaks.
This matters when you are trying to capture blocks of text, stack traces, or multi-line snippets. If your pattern works on one line but breaks on pasted content, missing s is a common cause.
The Unicode flag: u
The u flag tells the engine to interpret the pattern with Unicode awareness. This is important for emoji, non-Latin characters, and advanced escapes that depend on Unicode code points.
Without u, character handling can be surprising, especially when a single visible symbol is represented internally by multiple code units.
Same pattern, different result
Consider the pattern ^cat.$ against a multi-line string. Without flags, it only checks the entire input as one string and the dot will not cross line breaks. Add m and the anchors start working per line. Add s and the dot can now consume newline characters too.
This is why regex debugging is rarely just about the pattern itself. The matching mode matters just as much as the characters you typed.
Common mistakes
Many developers enable g when they really need m, or expect . to cross line breaks without s. Another common mistake is forgetting i when testing mixed-case input.
If the result looks inconsistent, test the exact same pattern with flags toggled one by one. That isolates whether the bug is in the expression or in the matching mode.
Debug it with the Regex Tester
Use the Regex Tester to toggle flags interactively and see how the same input changes under different modifiers. That is the fastest way to understand whether your regex logic is wrong or whether the right flag is simply missing.
Keep Reading In This Topic
Adjacent guides that support the same workflow or query family.
Common Regex Mistakes With Escaping and Greediness
A practical guide to the regex mistakes developers make most often under pressure.
Regex vs Wildcard Matching: When They Are Not Interchangeable
A practical comparison of wildcard matching and regular expressions for common developer workflows.