Regular Expression Flags Explained: g, i, m, s, u, y

Published on · 930 words

Want to follow along with this guide? Open the free Regex Tester and test every pattern in real time with match highlighting and explanations.

Regular expression flags modify how the regex engine interprets and executes a pattern. In JavaScript, six flags are available: g (global), i (case-insensitive), m (multiline), s (dotAll), u (Unicode), and y (sticky). Each flag unlocks different behavior, and combining them lets you fine-tune matching to your exact needs.

Understanding flags is essential for writing correct regex patterns. A pattern that works perfectly without the global flag might only find the first match, or a pattern that matches correctly on a single-line string might fail on multiline input. This guide explains each flag in detail with practical examples you can try in our Regex Tester.

The Global Flag (g) and Case-Insensitive Flag (i)

The g flag tells the regex engine to find all matches in a string, not just the first one. Without it, methods like exec() and match() return only the first match. With g, they iterate through every non-overlapping match. This is essential when you need to count occurrences, replace all instances, or extract every matching substring.

The i flag makes matching case-insensitive. With this flag, /hello/i matches 'hello', 'Hello', 'HELLO', and any other case variation. It is invaluable when matching user input where case should not matter, such as validating commands, keywords, or identifiers.

These two flags are the most commonly used and can be combined freely. For example, /error/gi finds all occurrences of 'error' regardless of case throughout the entire string. In our Regex Tester, simply click the g and i buttons to enable them.

  • g (global) — find all matches, not just the first
  • i (case-insensitive) — ignore upper/lowercase differences
  • Combined example: /error/gi finds all 'error' variations
  • g is required for String.prototype.replaceAll() with regex

Multiline (m), DotAll (s), and Unicode (u) Flags

The m flag changes how ^ and $ anchors behave. Normally, ^ matches only the start of the entire string and $ matches only the end. With multiline mode, ^ and $ also match at the start and end of each line within the string. This is essential when processing multiline text like log files, CSV data, or source code.

The s flag (dotAll, introduced in ES2018) changes how the . metacharacter works. Normally, . matches any character except newline characters. With the s flag, . also matches newlines. This is useful when you want a pattern like .+ to span across line breaks, such as matching a block of text between delimiters.

The u flag enables full Unicode matching. Without it, regex treats each UTF-16 code unit separately, which breaks matching for characters outside the Basic Multilingual Plane (like emoji or rare CJK characters). With u, the engine treats surrogate pairs as single characters, and Unicode property escapes like \p{Letter} become available.

The Sticky Flag (y) and Combining Flags

The y flag (sticky) is the least commonly used but can be powerful in specific scenarios. A sticky regex only matches at the position indicated by its lastIndex property. Unlike the global flag, which searches forward from lastIndex, the sticky flag requires the match to start exactly at lastIndex. This makes it ideal for tokenizers and parsers that process input sequentially.

Flags can be combined in any order. Common combinations include: gi for case-insensitive global search, gm for multiline global matching, and gu for Unicode-aware global matching. In JavaScript, you pass flags as the second argument to the RegExp constructor: new RegExp('pattern', 'gi').

You can experiment with all six flags in our Regex Tester. Toggle each flag button to see how it changes the matching behavior on your test string. The match details panel updates in real time, showing exactly which matches are found with the current flag combination.

  • m (multiline) — ^ and $ match line boundaries, not just string boundaries
  • s (dotAll) — . matches newline characters too
  • u (Unicode) — enables proper Unicode handling and property escapes
  • y (sticky) — matches only at lastIndex position, ideal for tokenizers
  • Combine freely: new RegExp(pattern, 'gimsuy')

Frequently Asked Questions

What happens if I use the g flag with exec()?
With the g flag, each call to exec() returns the next match and advances the lastIndex property. When no more matches are found, exec() returns null and resets lastIndex to 0. This lets you iterate through all matches in a while loop.
When should I use the multiline flag?
Use the m flag when your input contains multiple lines and you want ^ and $ to match the start and end of each line, not just the start and end of the entire string. This is common when processing log files, multi-line text areas, or any input with newline characters.
What is the difference between the s flag and the m flag?
The s flag (dotAll) changes how . works — it makes . match newline characters. The m flag (multiline) changes how ^ and $ work — they match line boundaries instead of just string boundaries. They solve different problems and can be used together.
Do I need the Unicode flag for normal text?
For basic ASCII text, the u flag is not necessary. However, if your input may contain emoji, accented characters from supplementary planes, or characters from non-Latin scripts, the u flag ensures correct matching. It also enables Unicode property escapes like \p{Script=Han}.
Can I see how flags affect matching in real time?
Yes. Our free Regex Tester lets you toggle all six flags (g, i, m, s, u, y) and see matches update instantly. The match details panel shows exactly which matches are found, their positions, and captured groups for the current flag combination.

Try it now — free, private, and instant

Paste any regex pattern, toggle flags, and see matches highlighted in real time.

Launch the Regex Tester