You can write a pattern that looks correct, test it, and still get a result that makes no sense: one match when the text clearly contains four, or no match on a line that starts with the right word. Very often the pattern is fine and the flags are wrong: the small letters alongside a pattern that quietly redefine what matching means. A pattern is really two things, the pattern text and the flags it runs under, and most "the regex is broken" moments are really "the regex is running under flags I did not choose".

This guide explains the five flags you will use most, g, i, m, s and u, each with a small tested example, reproducible in the free Regex Tester on Ovatly, where flags go in their own field.

Contents

  • Quick answer
  • The g flag: every match, not just the first
  • The i flag: case stops mattering
  • The m flag: anchors work per line
  • The s flag: the dot crosses line breaks
  • The u flag: matching in Unicode code points
  • How to set flags in the tester, step by step
  • Worked example: one text, five flags
  • Limitations and alternatives
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

Flags change the rules a pattern plays by. g reports every match instead of only the first. i makes matching case-insensitive. m makes ^ and $ match at line boundaries, not just at the ends of the whole text. s lets the dot match line breaks as well as ordinary characters. u makes the pattern count in Unicode code points, so emoji count as single characters. In the Regex Tester, type the pattern without slashes in the Pattern field and put the flags in the Flags field.

The g flag: every match, not just the first

The g flag stands for global. Without it, the tester reports only the first match, matching how most code uses a pattern: a validation check usually cares about the first hit, or whether one exists. With g, every match is listed with its text, index and groups.

Reach for it when counting matches or extracting all of them, and suspect it when a result list looks short: if you expected four matches and see one, check the Flags field first.

The i flag: case stops mattering

The i flag makes matching case-insensitive, so a pattern written in lowercase also matches its uppercase and mixed-case forms. It matters whenever text comes from humans or systems you do not control: log levels written as Error by one service and error by another.

The trade-off: i can hide real differences. If case carries meaning in your data, such as in case-sensitive codes, loose matching accepts strings you meant to reject.

The m flag: anchors work per line

The anchors ^ and $ normally mean the start and end of the whole text. With the m flag, for multiline, they mean the start and end of each line. The difference shows up only when your test text has more than one line: log files, pasted documents.

Without m, a pattern like ^start matches only at the very beginning of the text; with m, at the beginning of every line. On single-line text m changes nothing, which is why its absence goes unnoticed until the pattern meets real data.

The s flag: the dot crosses line breaks

The dot matches almost any character, but by default it does not match a line break. A pattern that tries to span two lines, matching a start marker on one line and an end marker on the next, fails silently because the dot stops at the break.

The s flag, sometimes called dotAll, removes that exception: with s, the dot matches line breaks too. Reach for it when what you are matching can contain newlines, such as text between two markers. Leave it off when a match should never cross a line: that restriction is often what keeps a greedy dot from swallowing half a document.

The u flag: matching in Unicode code points

JavaScript strings are stored as UTF-16 code units, and characters outside the basic set, including most emoji, take two units as a surrogate pair. Without the u flag, a pattern sees those units separately, so a dot matched against an emoji returns only the first half of the pair: a broken character.

The u flag switches the pattern to Unicode mode, where matching works in code points. The dot then matches the whole emoji as a single character, and indexes behave the way you would expect for text containing symbols or emoji. If your text can contain anything beyond plain ASCII, testing with u on is the safer default.

How to set flags in the tester, step by step

  1. Open the Regex Tester.
  2. Type your pattern in the Pattern field, without slashes.
  3. Type the flags you want in the Flags field: g, i, m, s, u and y, alone or combined as gi or gm.
  4. Paste or type your test text, including lines that should match and lines that should not.
  5. Read the match list: each match shows its text, index and groups. If the list surprises you, change one flag at a time and watch what changes.

Worked example: one text, five flags

Start with the tester's own example. Pattern \d+ against the text Order 123 ships in 45 days. With no flags, the tester reports a single match: 123 at index 6. Add g and the list gains the second match, 45 at index 19.

Now the other four. Pattern error against Error: disk full. error logged twice: one match with g alone (the lowercase error), two with gi. Pattern ^start on a three-line text where two lines begin with start: one match, two with gm. Pattern begin.end against the two-line text begin then end: no match until s is added. A single dot against an emoji: half a surrogate pair without u, the whole emoji with it.

Limitations and alternatives

The tester runs JavaScript's RegExp engine, so these flag meanings follow JavaScript rules, which differ slightly from PCRE, Python or grep flavours. Not every engine has the same flag set: the s flag is recent in JavaScript, and some engines spell these options differently or set them inline. Re-test in the flavour you will ship: a combination that behaves here may need adjusting when ported.

Results stop at 500 matches, so the tester is a tool for checking behaviour on samples, not for processing a whole file. And pathological patterns, such as nested quantifiers on long text, can cause catastrophic backtracking and make the tab busy; simplify the pattern if that happens. For bulk work, prove the pattern and flags here, then apply them in your editor or code.

Common mistakes

  • Typing flags into the Pattern field. Flags belong in their own field; typed into the pattern, the letters become literal characters the pattern tries to match.
  • Forgetting g and blaming the pattern. One match reported usually means the flag is missing, not that the pattern stopped working after the first hit.
  • Assuming flags carry over. Flags set while testing do not follow the pattern into your code. Wherever the pattern ends up, set the same flags explicitly.
  • Leaving i on out of habit. Case-insensitive matching accepts strings you may have meant to reject. Turn it on because case does not matter in this data, not by default.
  • Testing multi-line behaviour on one line. The m and s flags only show their effect on text with line breaks; if your real data has them, your test text must too.

FAQs

Where do I put the flags? In their own field, separate from the pattern. Type the pattern body without slashes in the Pattern field and the flags, such as g or gi, in the Flags field. They are never part of the pattern text itself.

Why is only the first match shown? Because the g flag is missing: without it, only the first match is reported, matching how most code uses a pattern. Add g in the Flags field to list every match, up to the limit of 500.

What does the u flag actually change? It switches the pattern from UTF-16 code units to Unicode code points. Without u, a dot matched against an emoji returns half of its surrogate pair, a broken character; with u, the whole emoji as one character.

Why did my results stop at exactly 500 matches? That is the tester's cap, there so a loose pattern cannot flood the page. A list stopping at exactly 500 usually means the pattern matches far more than you intended, so tighten it and test again.

Is my test text uploaded anywhere? No. The Regex Tester runs in your browser on your device, so the patterns and text you test with stay local.

Conclusion

Flags are not decoration; they are half of what a pattern means. Choose them as deliberately as you choose the pattern itself: g for all matches, i when case does not matter, m for line anchors, s when a match may cross a line break, and u whenever the text can hold characters beyond ASCII. For the companion chore, converting dates rather than matching text, see How to Convert a Date to a Unix Timestamp, and browse the rest of the Developer Tools guides for the remaining tools in this category.