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 i so error also finds ERROR and Error.
  • Multiline log matching — add m so ^ anchors to the start of each line in a block of text.
  • Matching across newlines — add s so . 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 u to 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?

They are constantly confused. 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?

No. JavaScript has never supported the extended/verbose flag, so you cannot add inline whitespace or comments to a pattern. Python, PCRE, and .NET do; in JavaScript you build the pattern from a commented string instead.

Why does <code>test()</code> return alternating true/false in JavaScript?

When a regex carries the 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?

Whenever you use \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.
Embed this tool on your site

Free to embed, no attribution required (but appreciated). Paste this where you want the tool to appear: