The m flag (multiline) changes how ^ and $ work. They match the start and end of each line, not only the start and end of the whole string—ideal for logs, configs, and multi-line input.
Remember
/pattern/m // multiline anchors
regex.multiline // true when m is set
without m: ^ = start of string, $ = end of string
with m: ^ = start of each line, $ = end of each line
m does NOT make . match newlines (that is the s / dotAll flag)
combine: /^.+$/gm
Use
Prefer
Line starts with…
/^#/m or /^#/gm
Line ends with…
/end$/m
All non-empty lines
text.match(/^.+$/gm)
Approach
How to Use the m Flag
Add m whenever your pattern uses ^ or $ per line.
Create a multiline regex: /^…/m, /…$/m, or new RegExp("^…", "m").
Put newlines in the string (\n) so line boundaries exist.
re = /^pattern$/m
text has lines split by "\n"
if m is off:
^ only at index 0
$ only at last index
if m is on:
^ also after each "\n"
$ also before each "\n" (and at end)
test / match as usual
Reference
Cheat Sheet
Keep these multiline facts handy.
Topic
Detail
Syntax
/pattern/m or new RegExp(p, "m")
Check flag
regex.multiline
^ with m
Start of string and after each newline
$ with m
End of string and before each newline
With global
/pattern/gm
vs s (dotAll)
m = anchors; s = . matches newline
Standard
ES3+ (universal)
Try it
Live Preview
Compare anchors with and without m on multi-line sample text.
Browser demo of multiline anchors — same idea as the examples below.
Result
text = "nope\\nstart here\\nstart again"
/^start/m.test(text)
→ true
/* m lets ^ match after each newline */
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — ^ With and Without m
Match a word at the start of any line, not only the start of the string.
^ and $ per line. With m, each line is its own start/end.
.+ skips empty lines.. does not match newline, so each match is one line of text.
g finds every line.match returns all three fruit names.
Practical pattern./^#.*/gm finds comment lines that start with #.
Edge Cases & Pitfalls
Check these before relying on multiline anchors.
name
m is not “match across lines”
It only changes ^ and $. The pattern still does not cross newlines with . unless you add the s (dotAll) flag.
newlines
Need real line breaks
Without \n (or other line terminators) in the string, m behaves like a normal single-line match.
crlf
\r\n line endings
Windows text may use \r\n. Anchors still work at line boundaries; normalize with care if you also strip \r.
empty
/^$/gm matches empty lines
Use /^.+$/gm when you want only non-empty lines.
Compatibility
Browser & Runtime Support
The m (multiline) flag is part of core JavaScript regular expression syntax (ES3+).
✓ Baseline · ES3+
RegExp m / multiline
Supported in every browser and Node.js version. Line-based ^ and $ work everywhere.
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
m flagExcellent
Bottom line: Safe everywhere. Use m whenever ^ or $ should mean “per line,” and add g when you need every line.
Analysis
Performance Notes
Approach
Cost
Best for
/^…/gm
O(n)
Scanning every line with anchors
split("\n") + loop
O(n)
When you need full line objects anyway
/^.+$/gm
O(n)
Collecting non-empty lines quickly
Multiline matching still walks the string once. Prefer /^…/gm for simple line filters; use split when you will process every line with more than a regex.
Remember
Key Takeaways
Role:m makes ^ / $ mean start/end of each line.
Check:regex.multiline mirrors the flag.
Combo: use gm to collect matches from every line.
Not dotAll:m ≠ s; . still stops at newlines.
One line: add m when ^ / $ should mean “per line,” not “whole string.”
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'). Then regex.multiline is true.
No. The m flag affects ^ and $ only. To make . match newline characters, use the s (dotAll) flag: /pattern/s.
Use gm when you need ^ or $ matches on every line—for example text.match(/^.+$/gm) to collect non-empty lines, or /^#/gm to find comment lines.
Without m, ^ matches only the very beginning of the string. With m, ^ also matches immediately after each newline.
The multiline flag is core JavaScript regex syntax since ES3. It works in every browser and Node.js version.