JavaScript RegExp m Flag

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is the m Flag?

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
UsePrefer
Line starts with…/^#/m or /^#/gm
Line ends with…/end$/m
All non-empty linestext.match(/^.+$/gm)

How to Use the m Flag

Add m whenever your pattern uses ^ or $ per line.

  1. Create a multiline regex: /^…/m, /…$/m, or new RegExp("^…", "m").
  2. Put newlines in the string (\n) so line boundaries exist.
  3. Add g when you need every line: /^…/gm.
  4. Confirm with regex.multiline when debugging.
PieceRequired?Role
m flagYesLine-based ^ / $
^ / $UsualAnchors that m redefines
g flagOptionalCollect matches from every line

Pseudocode

Pseudocode
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

Cheat Sheet

Keep these multiline facts handy.

TopicDetail
Syntax/pattern/m or new RegExp(p, "m")
Check flagregex.multiline
^ with mStart of string and after each newline
$ with mEnd of string and before each newline
With global/pattern/gm
vs s (dotAll)m = anchors; s = . matches newline
StandardES3+ (universal)

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 */

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.

JavaScript
const text = "nope\nstart here\nstart again";

console.log(/^start/.test(text));     // false
console.log(/^start/m.test(text));    // true
console.log(text.match(/^start/gm));  // ["start", "start"]
console.log(/^start/m.multiline);     // true
Try It Yourself

How It Works

  1. Without m. ^ only checks index 0 — the string starts with "nope" → false.
  2. With m. ^ also matches after each \n, so line 2 succeeds → true.
  3. Add g. /^start/gm collects the start of every matching line.
  4. Check the flag. multiline is true when m is set.

Example 2 — $ With and Without m

Match text at the end of a line, even when it is not the end of the string.

JavaScript
const text = "line one end\nline two end\nno match";

console.log(/end$/.test(text));     // false
console.log(/end$/m.test(text));    // true
console.log(text.match(/end$/gm));  // ["end", "end"]
Try It Yourself

How It Works

  1. Without m. $ only matches the very end — the string ends with "no match" → false.
  2. With m. $ also matches before each newline, so lines 1 and 2 succeed.
  3. gm collects both. You get ["end", "end"] from the first two lines.
  4. Same idea as ^. Multiline redefines both anchors together.

Example 3 — Collect Every Non-Empty Line

Use /^.+$/gm to pull each line of text into an array.

JavaScript
const text = "apple\nbanana\ncherry";
const lines = text.match(/^.+$/gm);

console.log(lines);         // ["apple", "banana", "cherry"]
console.log(lines.length);  // 3

// Comment lines in a config:
const cfg = "# title\nname=js\n# end";
console.log(cfg.match(/^#.*/gm));  // ["# title", "# end"]
Try It Yourself

How It Works

  1. ^ and $ per line. With m, each line is its own start/end.
  2. .+ skips empty lines. . does not match newline, so each match is one line of text.
  3. g finds every line. match returns all three fruit names.
  4. 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.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
m flag Excellent

Bottom line: Safe everywhere. Use m whenever ^ or $ should mean “per line,” and add g when you need every line.

Performance Notes

ApproachCostBest for
/^…/gmO(n)Scanning every line with anchors
split("\n") + loopO(n)When you need full line objects anyway
/^.+$/gmO(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.

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.

Next: $ end anchor

Learn how $ marks the end of input (and each line with m).

$ end anchor tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful