Back to the tool

The Regex Multiline Flag, Explained

The regex multiline flag is one letter, m, and it fixes one of the quietest bugs in programming. You write a pattern that reads perfectly, run it against a log file, and get zero matches. No error. No warning. Just silence, because ^ and $ were never doing what you assumed.

Here is the bug in its natural habitat. You have a log file, one entry per line, and you want every line that starts with a digit:

const text = "1. First item\n2. Second item\n3. Third item";
text.match(/^\d/g);   // ["1"] — only the first line
text.match(/^\d/gm);  // ["1", "2", "3"] — every line

Same pattern, same text, different flag, different answer. The first version found one match because ^, without m, anchors to the start of the entire string. There is exactly one start-of-string, so there is exactly one possible match. The m flag tells the anchors to also match after every newline and before every newline. That is the whole flag. Nothing else in the pattern changes.

Regex multiline flag explained: the mental model

Think of ^ and $ as positions, not characters. By default, there are two positions in the universe: before the first character and after the last one. The multiline flag adds two more positions around every line break: after each \n and before each \n. The pattern itself is untouched. \d+ still matches digits the same way; only where the pattern is allowed to start and stop has changed.

This is why the flag is silent when your string has no newlines. Multiline on a single-line string changes nothing, because there are no line boundaries to add. If you are debugging a pattern and the m flag seems to do nothing, check whether the input actually contains line breaks. Half the time it does not.

The two traps that come with it

Trap 1: confusing multiline with dotall. The m flag changes only the anchors. It does not change ., which still refuses to match newline characters. The flag that lets . cross line breaks is s, dotall, in JavaScript and Python. These two get mixed up constantly because they both involve the word "line." Mnemonic: m is about where lines begin and end; s is about what counts as a character.

Trap 2: validating with multiline on. This one is a genuine security-shaped bug. Suppose you validate a form field with /^\d+$/m to accept only digits. An input like "abc\n123" passes, because ^ matches after the newline and $ matches at the end. Your "digits only" check just accepted letters. If the input must be a single value, validate without m, or strip newlines before the check. Multiline is for processing multi-line text, not for validating a single line.

How the flag looks across languages

LanguageEnable multilineDefault anchor behavior
JavaScript/pattern/mString boundaries
Pythonre.MULTILINEString boundaries
JavaPattern.MULTILINEString boundaries
PHP/pattern/mString boundaries
.NETRegexOptions.MultilineString boundaries
Ruby / Boost / most editorsNot neededLine boundaries by default

The last row is worth remembering. In Ruby, in Boost, and in tools built for editing whole files like VS Code's search, ^ and $ match line boundaries without any flag. A pattern that works in your editor and fails in your JavaScript has not changed. The engine's default did.

So the next time a pattern returns nothing on multi-line input, check one letter before you rewrite anything. Nine times out of ten, the pattern was right and the anchors were anchored to the wrong universe.

Frequently asked questions

What does the regex multiline flag do?

It changes what ^ and $ anchor to. By default they match the start and end of the entire string. With the m flag, they also match at the start and end of each line within the string. Nothing else in the pattern changes.

Why does ^ not match the start of each line in JavaScript?

Because JavaScript's ^ anchors to the start of the whole string unless the pattern has the m flag. Use /^\d+/gm to match a digit at the start of every line. This is the single most common ^ and $ confusion.

Does the multiline flag change how . matches newlines?

No. That is the dotall flag, s in JavaScript and Python. Multiline only affects ^ and $. Confusing the two is the second most common regex flag mistake.

Do all regex engines support the multiline flag?

Most do, but the default differs. In Ruby, Boost, and most text editors, ^ and $ match line boundaries by default, so the flag is unnecessary. In JavaScript, Python, Java, PHP, and .NET you must opt in: the m flag, re.MULTILINE, Pattern.MULTILINE, or RegexOptions.Multiline.

Should I use multiline for validating form input?

Be careful. /^\d+$/m accepts "abc\n123" because ^ and $ match at internal line boundaries. If the input must be a single line, validate without m, or strip newlines first. Multiline is for processing multi-line text, not for validating a single value.

Related reading: Regex Greedy vs Lazy Matching: The One Question Mark That Changes Everything · Regex Lookahead and Lookbehind Explained: The Zero-Width Trick · Regex Catastrophic Backtracking: What It Is and How to Avoid It

Try the tool

Anchors behave differently in testers that hide the flags. Build and test your regex on the homepage with explicit flag controls, so the pattern you ship matches the pattern you tested.

Keep reading

Regex Greedy vs Lazy Matching: The One Question Mark That Changes Everything

Get new free tools by email

Want the next guide in your inbox? I publish one practical guide per new tool. Subscribe to the free newsletter on Substack. No spam, unsubscribe anytime.