Regex Flags Explained
understand what regex flags g, i, m, s, u, y do with live examples
Interactive Regex Flags Explained — coming soon
In development. We're still building the interactive Regex Flags Explained. The guide below explains exactly what it does and how to use it. In the meantime, here are DevLab tools that already work in your browser:
Browse all working tools →Regex flags change how a pattern matches without altering the pattern itself. The global flag (g) makes /cat/g find every occurrence in a string instead of stopping after the first; without it, 'catcat'.match(/cat/) returns only one result. The s (dotAll) flag is the one most developers forget exists — it makes . match newline characters, which it silently skips by default.
What is Regex Flags Explained?
Regex Flags Explained is an interactive reference that runs each ECMAScript flag in isolation and in combination, showing you live how the same pattern behaves differently with and without each modifier. Paste a pattern and a test string, toggle flags one at a time, and the match output updates immediately so you can see, for example, that adding i to /hello/ makes it match 'Hello', 'HELLO', and 'hElLo' without touching the pattern.
Developers typically reach for regex101 when they need a full-featured debugger with a match-step visualizer and regex flavor switching (PCRE, Python, Java). This tool is narrower: it focuses exclusively on JavaScript's six flags and is useful when you already know your pattern works but you're unsure which combination of flags to ship with it, or when you want to teach a colleague what m actually does to ^ and $.
The most common confusion with flags involves m versus s. The multiline flag (m) does not make . span lines — it only changes where ^ and $ anchor. To make . cross a newline you need s, which was added in ECMAScript 2018 and is unavailable in Node.js below v10. The y (sticky) flag is subtler still: it respects lastIndex and will fail a match rather than scanning forward, which makes it useful for tokenizers but confusing in one-off scripts.
When to use Regex Flags Explained
- Debug a regex that stops matching after the first hit by confirming whether the `g` flag is missing.
- Verify that your multiline pattern's `^` anchors are matching line starts rather than only the string start before deploying.
- Audit a third-party regex you've inherited to understand which flags it expects before integrating it into a parser.
Frequently Asked Questions
Why does adding the `g` flag break my `exec()` loop?
Does the `u` flag change which characters `\w` and `\d` match?
Why does `/^line/m` match in the middle of my string?
When would I actually use the `y` (sticky) flag over `g`?
Is the `s` (dotAll) flag safe to use in all browsers today?
Related Tools
- Regex Testertest and debug regular expressions with real-time matching · Regex
- Regex Cheatsheetbrowse a complete regex syntax reference with live examples · Regex
- Regex Explainerbreak down a regex pattern into plain English with visual diagrams · Regex
- Lookahead & Lookbehind Guidelearn lookahead and lookbehind assertions with interactive examples · Regex