The m modifier (multiline flag) changes how ^ and $ anchors work. Instead of matching only the start and end of the entire string, they match the start and end of each line—making it easy to parse config files, logs, and multi-line user input line by line.
01
m flag
Multiline
02
^ line
Line start
03
$ line
Line end
04
gm
All lines
05
Not dot
. still skips \n
06
multiline
Property
Fundamentals
Introduction
Real-world text often spans multiple lines: CSV rows, `.env` files, stack traces, and user comments in textareas. By default, ^ matches only the very beginning of the string and $ only the very end—so a pattern like /^#/ finds comment lines only if the string starts with #.
Add the m modifier and those anchors apply at each line boundary. Now /^#/ matches any line that begins with #, which is the behavior most developers expect when working with multi-line text.
Concept
Understanding the m Modifier
Append m to enable multiline mode. The RegExp sets multiline: true, and ^ matches after a newline (or at string start), while $ matches before a newline (or at string end).
JavaScript
const text = "start here\nnot start\nstart again";
/^start/.test(text); // true (first line only)
/^start/m.test(text); // true (lines 1 and 3)
text.match(/^start/gm); // ["start", "start"]
The m flag does not change how . behaves—the dot still does not match newline characters unless you also use the s (dotAll) flag.
💡
Beginner Tip
When parsing line-by-line with ^ or $, add m. When you need . to cross newlines, add s instead (or in addition).
Usage
How to Use the m Modifier
Enable multiline mode on literals, constructors, and combined flags:
JavaScript
/^line/m; // multiline literal
new RegExp("^line", "m"); // constructor
/^.+$/gm; // every full line
re.multiline; // true when m is set
config.match(/^#.*$/gm); // all comment lines
Pair m with g when you need to match anchors on every line, not just the first occurrence.
Foundation
📝 Syntax
JavaScript
/pattern/m // multiline anchors
/pattern/gm // global + multiline
/pattern/gim // global + ignoreCase + multiline
new RegExp("p", "m") // constructor
regex.multiline // true | false
Common patterns
/^#.+$/gm — match every comment line.
/^\s*$/gm — find blank lines.
/^ERROR/m — detect lines starting with ERROR.
/^.+$/gm — split string into logical lines via match.
Cheat Sheet
⚡ Quick Reference
Goal
Pattern / API
Enable multiline
/pattern/m
Check flag
regex.multiline
Line starts with text
/^text/m
Line ends with text
/text$/m
All full lines
/^.+$/gm
Dot matches newline
/pattern/s (not m)
Compare
📋 With m vs without m
How ^ and $ behave on multi-line text.
No m
^
String start only
With m
^ + m
Each line start
No m
$
String end only
With m
$ + m
Each line end
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. Examples cover ^ line start, $ line end, full-line extraction, multiline property, and comment-line detection.
For string-wide anchors without line mode, omit m.
Flag order is free: gm equals mg.
Compatibility
Browser & Runtime Support
The m multiline modifier is part of core JavaScript regular expression syntax.
✓ Baseline · ES3+
RegExp m flag
Supported in every browser and Node.js version. The m flag enables line-based ^ and $ matching.
99%Universal syntax
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
mExcellent
Bottom line: Safe everywhere. Use m when ^ or $ should apply to each line in multi-line text.
Wrap Up
Conclusion
The m modifier enables multiline anchor behavior: ^ and $ match line boundaries, not just the string edges. It is essential for parsing configs, logs, and any text with embedded newlines.
Combine gm to process every line, remember that m does not change dot behavior, and reach for the s flag when you need . to span newlines. Next, learn the $ end quantifier.
Normalize \r\n to \n when parsing cross-platform files
Check multiline when debugging anchor behavior
Consider split("\n") as a simple alternative for line lists
❌ Don’t
Expect m to make . match newlines
Forget g when you need all line matches
Use m when you only care about the whole string edges
Confuse multiline (m) with global (g)
Assume m splits the string automatically
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about the RegExp m modifier
Match line-by-line with ^ and $ on multi-line strings.
5
Core concepts
📄01
m flag
Multiline.
Syntax
📈02
^ $
Per line.
Anchors
🚫03
Not .
Use s flag.
Pitfall
🔒04
gm
All lines.
Combine
🔢05
# lines
Comments.
Use case
❓ Frequently Asked Questions
The m flag (multiline) changes how ^ and $ work. They match the start and end of each line (at newline boundaries), not only the start and end of the entire string.
Append m to the literal: /pattern/m, combine flags like /pattern/gm, or pass 'm' in new RegExp('pattern', 'm'). The multiline property on the instance becomes true.
No. The m flag affects ^ and $ anchors only. To make . match newline characters, use the s (dotAll) flag: /pattern/s.
Use gm when you need to find ^ or $ matches on every line in a string—for example, extracting all lines with match(/^.+$/gm) or finding every line that starts with #.
Without m, ^ matches only the very beginning of the string. With m, ^ also matches the position immediately after each newline character.
The multiline flag is core JavaScript regex syntax since ES3. It works in every browser and Node.js version.
Did you know?
The name “multiline” confuses many beginners because it sounds like the regex matches across lines. In JavaScript, m only changes ^ and $—the pattern itself still cannot cross a newline with . unless you add the separate s (dotAll) flag.