Regex Flags
JavaScript regex flags modify how the pattern is interpreted and matched. RegexLens supports all six flags.
Flag Reference
g — Global
Find all matches instead of stopping after the first.
Without g | With g |
|---|---|
| Returns first match only | Returns all matches |
"abc".match(/a/) → ["a"] | "aba".match(/a/g) → ["a", "a"] |
When to use:
- Finding all occurrences in text
- Search and replace all
- Counting matches
i — Case Insensitive
Match letters regardless of case.
Without i | With i |
|---|---|
a matches only a | a matches a and A |
[A-Z] matches uppercase | [A-Z] matches any letter |
When to use:
- User input validation (emails, usernames)
- Searching text content
- Case-insensitive comparisons
m — Multiline
Changes behavior of ^ and $ anchors.
Without m | With m |
|---|---|
^ matches start of string | ^ matches start of each line |
$ matches end of string | $ matches end of each line |
When to use:
- Processing multi-line text
- Matching at line boundaries
- Log file parsing
s — DotAll (Single Line)
Makes . match newline characters.
Without s | With s |
|---|---|
. matches any char except \n | . matches any char including \n |
a.b doesn't match a\nb | a.b matches a\nb |
When to use:
- Matching across line breaks
- Processing text blocks
- HTML/XML parsing (with caution)
u — Unicode
Enables full Unicode support.
Without u | With u |
|---|---|
\u{1F600} is invalid | \u{1F600} matches 😀 |
. may not match emoji | . matches any Unicode char |
\p{L} is invalid | \p{L} matches any letter |
When to use:
- International text processing
- Emoji handling
- Unicode property escapes (
\p{...})
Unicode property escapes (with u flag):
\p{L}— Any letter\p{N}— Any number\p{Emoji}— Any emoji\p{Script=Greek}— Greek letters
y — Sticky
Matches only at the lastIndex position.
Without y | With y |
|---|---|
| Searches entire string | Only matches at current position |
lastIndex is suggestion | lastIndex is required position |
When to use:
- Tokenizers/lexers
- Sequential parsing
- State machine implementations
Example:
const re = /\d+/y;
const str = "123abc456";
re.lastIndex = 0;
re.exec(str); // ["123"], lastIndex = 3
re.exec(str); // null (no digit at position 3)
re.lastIndex = 6;
re.exec(str); // ["456"]Flag Combinations
Common Combinations
| Flags | Use Case |
|---|---|
gi | Find all, case-insensitive (most common) |
gm | Find all across multiple lines |
gim | Find all, case-insensitive, multiline |
gis | Find all, case-insensitive, dotAll |
gu | Find all with Unicode support |
Examples
Find all emails (case-insensitive):
/[\w.+-]+@[\w.-]+\.[a-z]{2,}/giMatch lines starting with #:
/^#.+$/gmMatch any character including newlines:
/start.*end/gsFlag Order
Flags can be specified in any order:
gi=iggim=mig=gmi
RegexLens normalizes flag display to alphabetical order.
Browser Support
All flags are supported in modern browsers:
| Flag | Chrome | Firefox | Safari | Edge |
|---|---|---|---|---|
g | ✓ | ✓ | ✓ | ✓ |
i | ✓ | ✓ | ✓ | ✓ |
m | ✓ | ✓ | ✓ | ✓ |
s | 62+ | 78+ | 11.1+ | 79+ |
u | 50+ | 46+ | 10+ | 79+ |
y | 49+ | 3+ | 10+ | 79+ |
Tips
Performance tip: The g flag with exec() maintains state via lastIndex. Reset it between uses or create a new RegExp.
Global Flag Gotcha
const re = /a/g;
re.test("abc"); // true, lastIndex = 1
re.test("abc"); // false, lastIndex = 0 (reset after no match)
re.test("abc"); // true againUnicode Flag Requirement
Some features require the u flag:
\u{...}code point syntax\p{...}property escapes- Correct surrogate pair handling