Regex flags — what each one does
Flags are single-character modifiers that change how the whole regex behaves. This is the complete reference with examples and language-specific notes.
g — global
Without /g, most engines return only the first match. With /g, you get all matches.
"a 1 b 2 c 3".match(/\d/); // ["1"]
"a 1 b 2 c 3".match(/\d/g); // ["1", "2", "3"]
Python doesn't use a g flag — instead it has separate functions: findall/finditer for all matches, search/match for first.
i — case-insensitive
/cat/i matches "Cat", "CAT", "cAt". Applies to literal letters and to character classes:
"Hello World".match(/HELLO/i); // matches
For non-ASCII characters, behavior depends on Unicode mode. /ä/i may or may not match "Ä" depending on the engine and flags.
m — multiline
Without /m, ^ and $ match only at the start and end of the whole string. With /m, they also match at line boundaries.
const text = "line1\nline2\nline3";
text.match(/^line\d/gm); // ["line1", "line2", "line3"]
text.match(/^line\d/g); // ["line1"]
Does not change the meaning of \b or other anchors.
s — dotall (single-line)
The metacharacter . matches any character except newline by default. With /s, it matches newlines too.
"line1\nline2".match(/line1.line2/s); // matches
"line1\nline2".match(/line1.line2/); // null
Confusingly, this flag is called "single-line" in some communities and "dotall" in others. Same flag, same behavior. In Python it's re.DOTALL, in JavaScript s.
u — Unicode
JavaScript only. Treats the pattern as a Unicode-aware regex. Changes several things:
- Allows
\u{1F600}for code points above U+FFFF. - Enables Unicode property escapes:
\p{Letter},\p{Emoji}, etc. - Makes
.,\w,\saware of multi-code-unit characters. - Throws on invalid patterns rather than silently treating them as literal.
Generally enable /u for any pattern dealing with international text. Newer flag v (Unicode set notation) adds set operations inside character classes.
x — verbose / extended
Available in Python, Java, .NET, Perl, PCRE, Ruby. Not in JavaScript. Allows whitespace and # comments inside the pattern:
# Python
email_re = re.compile(r"""
^[a-z0-9._%+-]+ # local part
@ # at sign
[a-z0-9.-]+ # domain
\. # dot
[a-z]{2,}$ # TLD
""", re.X | re.I)
Inside character classes, whitespace is still literal. To include a literal space outside a class, escape it.
y — sticky (JavaScript)
Like /g, /y advances lastIndex, but it requires the match to start exactly at lastIndex (no skipping ahead).
const re = /\d+/y;
re.lastIndex = 4;
re.exec("abc 123"); // null — position 4 is "1"23 — wait let me recount
// Actually: "abc 123" — positions 0:a, 1:b, 2:c, 3:space, 4:1
// At position 4 it matches "123"
re.exec("abc 123"); // ["123"]
Useful when implementing tokenizers and parsers where you want to know if the next token matches at the current position.
d — hasIndices (JavaScript ES2022)
Adds an indices property to Match results, giving start/end positions for each capture group:
const m = "abc 123".match(/(\d+)/d);
m.indices; // [[4, 7], [4, 7]]
m.indices[0]; // [4, 7] — whole match
m.indices[1]; // [4, 7] — group 1
A — ASCII (Python)
By default in Python 3, \w matches Unicode letters. With re.A (or (?a) inline), it reverts to ASCII only.
Combining flags
In JavaScript: stack the letters: /pattern/gimsu.
In Python: bitwise OR the constants: re.compile(pattern, re.I | re.M | re.S).
In Java/.NET: same as Python — bitwise OR of options.
Inline flag modifiers
You can set or unset flags inside the pattern using (?flags) or (?flags:group):
(?i)CASE // case-insensitive for the rest of the pattern
(?-i)EXACT // case-sensitive
(?i:Hello)world // "Hello" is case-insensitive, "world" is not
Supported in PCRE, Python, Java, .NET, Ruby. Not in JavaScript.
Which flags affect what
| Flag | Affects |
|---|---|
| g | How many matches returned (all vs first) |
| i | Letter comparisons (a == A) |
| m | ^ and $ anchors |
| s | The . metacharacter |
| u | Unicode handling (escapes, character classes) |
| x | Whitespace and comments in pattern |
| y | Where match must start (JavaScript) |