Developer

Regex Flags Explained (g, i, m, s, u, y)

Dev Nexus4 min read

What each JavaScript regex flag does — g, i, m, s, u, y — with a short example of what changes when you toggle it.

Flags are the letters that trail a regular expression — the gi in /cat/gi. They do not change what the pattern looks for; they change how the engine searches. Getting them wrong is one of the most common reasons a regex "almost" works.

JavaScript has six flags you will actually use: g, i, m, s, u and y. This post explains each one with a short before-and-after example so you know exactly what toggling it does.

The Problem

Most regex tutorials focus on the pattern syntax and treat flags as an afterthought. So you write a solid pattern, it matches once when you expected it to match everywhere, and you assume the pattern is wrong.

It usually is not. The pattern is fine — the behaviour you want lives in the flags. Case sensitivity, matching across newlines, finding every occurrence, whether . includes line breaks: all of that is controlled by a single letter, and it is easy to reach for the wrong one or forget it entirely.

The Solution

Once you know what each flag changes, they stop being mysterious. The fastest way to internalise them is to toggle each one and watch the highlighting move. In the Regex Tester you can flip g, i, m, s, u and y on and off against the same text and see the match set change in real time, entirely in your browser.

Below is a quick reference; the steps that follow give a concrete example for each flag.

Step-by-Step Guide

  1. 1

    g — global

    Finds every match instead of stopping at the first. Against "cat cat cat", the pattern cat with no flags highlights one match; with g it highlights all three. This is the flag people forget most often. It is also required when you want to replace every occurrence rather than just one.

  2. 2

    i — case-insensitive

    Ignores letter case. cat normally misses "Cat" and "CAT"; with i, all three match. Reach for it whenever the casing of the input is not something you control, such as user-typed text or headers.

  3. 3

    m — multiline

    Makes the anchors ^ and $ match at the start and end of each line, not just the whole string. With input spanning several lines, ^\d+ with m matches a number at the start of every line; without m it only checks the very first line.

  4. 4

    s — dotAll

    Lets . match newline characters too. By default . matches everything except line breaks, so a.b fails across a newline. With s, a.b matches "a\nb". Useful when you want a pattern to span lines without listing every whitespace character.

  5. 5

    u and y — unicode and sticky

    u enables full Unicode mode: it makes \p{...} property escapes work and treats characters outside the basic plane (like many emoji) as single units. y is sticky — it only matches starting exactly at the engine's current position, which is handy for tokenizers and hand-written parsers that consume input piece by piece.

Common Mistakes

  • Assuming the pattern is broken when a flag is missing

    Only-one-match usually means a missing g; missed uppercase usually means a missing i. Before rewriting the pattern, check whether the behaviour you want is actually a flag.

  • Confusing m and s

    m changes what ^ and $ mean; s changes what . matches. They are unrelated. If you want a pattern to reach across newlines with ., you need s, not m.

  • Reusing a global-flagged regex and losing matches

    A regex with g keeps state in its lastIndex property between calls to .test() and .exec(). Reusing the same object in a loop can skip matches. Create a fresh regex, or reset lastIndex, when this bites.

  • Forgetting u when working with emoji or scripts

    Without u, characters outside the basic multilingual plane are treated as two code units, so . matches half a character. Add u whenever your text includes emoji, or you use \p{...} property escapes.

Frequently Asked Questions

Can I combine flags?

Yes. Flags stack in any order — `gi`, `gim`, `gsu` are all valid. Each one is independent, so combine as many as you need.

Which flag finds all matches?

The global flag, `g`. Without it the engine returns only the first match. It is the flag most often forgotten when a pattern matches once but should match many times.

What is the difference between m and s?

`m` (multiline) changes `^` and `$` to anchor at each line boundary. `s` (dotAll) changes `.` to also match newline characters. They solve different problems and are often confused.

Do I always need the u flag?

Not for plain ASCII text. Add `u` when your input contains emoji or non-BMP characters, or when you use Unicode property escapes like `\p{L}`, so the engine treats characters correctly.

How can I see what a flag does?

Toggle it in the Regex Tester and watch the highlighting change against the same text. Seeing the match set shift is the quickest way to understand any flag.

Try the Tool

Regex Tester

Toggle every flag against live text and watch the matches change instantly.

Open Regex Tester

Related Tools

Related Articles