Regex Flags Reference (i, g, m, s, u, y, x)
Regex flags (modifiers) change how a pattern matches — case, multi-line, dot-matches-newline, sticky, unicode. The set of supported flags differs between languages and so does their behavior. This reference lists every flag, what it does, and which languages support it.
How to use the Regex Flags Reference (i, g, m, s, u, y, x)
Browse or filter by keyword. Each flag card shows: letter, name, description, language support matrix (JavaScript / Python / Perl / PCRE), and an example before/after.
What regex flags change
Flags (also called modifiers) sit outside the pattern and change how the whole thing matches. The common ones are i (case-insensitive), g (find every match, not just the first), m (multiline: ^ and $ match at line breaks), s (dotall: . also matches newlines), u (full Unicode), y (sticky), and in some engines x (verbose) and A (anchored).
Which flags exist depends on the language: y is a JavaScript speciality, while x and A exist in PCRE and Python but not in JavaScript. Getting a flag wrong is one of the most common regex bugs, because the pattern still compiles — it just quietly matches the wrong thing.
Common use cases
- Case-insensitive search — add
isoerroralso findsERRORandError. - Multiline log matching — add
mso^anchors to the start of each line in a block of text. - Matching across newlines — add
sso.spans line breaks when you capture a whole block. - Readable patterns — use the verbose flag (where supported) to add whitespace and comments inside a long regex.
- Unicode text — add
uto handle emoji, surrogate pairs, and property escapes correctly.
Frequently asked questions
What is the difference between the <code>m</code> and <code>s</code> flags?
m (multiline) changes only ^ and $ so they match at line boundaries. s (dotall) changes only . so it also matches newlines. They are independent and can be combined.Does JavaScript have a verbose (<code>x</code>) flag?
Why does <code>test()</code> return alternating true/false in JavaScript?
g (or y) flag, JavaScript keeps a lastIndex between calls, so repeated test() calls walk through the string. Drop the g flag for a simple yes/no check.When do I actually need the <code>u</code> flag?
\p{...} property escapes, match characters outside the basic multilingual plane (many emoji), or want . to treat a surrogate pair as one character. Without u, those are handled as separate code units.