Reference
Regex Flags

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 gWith g
Returns first match onlyReturns 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

Try it → (opens in a new tab)


i — Case Insensitive

Match letters regardless of case.

Without iWith i
a matches only aa 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

Try it → (opens in a new tab)


m — Multiline

Changes behavior of ^ and $ anchors.

Without mWith 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

Try it → (opens in a new tab)


s — DotAll (Single Line)

Makes . match newline characters.

Without sWith s
. matches any char except \n. matches any char including \n
a.b doesn't match a\nba.b matches a\nb

When to use:

  • Matching across line breaks
  • Processing text blocks
  • HTML/XML parsing (with caution)

Try it → (opens in a new tab)


u — Unicode

Enables full Unicode support.

Without uWith 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

Try it → (opens in a new tab)


y — Sticky

Matches only at the lastIndex position.

Without yWith y
Searches entire stringOnly matches at current position
lastIndex is suggestionlastIndex 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

FlagsUse Case
giFind all, case-insensitive (most common)
gmFind all across multiple lines
gimFind all, case-insensitive, multiline
gisFind all, case-insensitive, dotAll
guFind all with Unicode support

Examples

Find all emails (case-insensitive):

/[\w.+-]+@[\w.-]+\.[a-z]{2,}/gi

Match lines starting with #:

/^#.+$/gm

Match any character including newlines:

/start.*end/gs

Flag Order

Flags can be specified in any order:

  • gi = ig
  • gim = mig = gmi

RegexLens normalizes flag display to alphabetical order.

Browser Support

All flags are supported in modern browsers:

FlagChromeFirefoxSafariEdge
g
i
m
s62+78+11.1+79+
u50+46+10+79+
y49+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 again

Unicode Flag Requirement

Some features require the u flag:

  • \u{...} code point syntax
  • \p{...} property escapes
  • Correct surrogate pair handling