JavaScript RegExp m Modifier

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Multiline flag

What You’ll Learn

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

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.

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).

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.

📝 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.

⚡ Quick Reference

GoalPattern / API
Enable multiline/pattern/m
Check flagregex.multiline
Line starts with text/^text/m
Line ends with text/text$/m
All full lines/^.+$/gm
Dot matches newline/pattern/s (not m)

📋 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

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.

📚 Getting Started

Match text at the start of lines with ^.

Example 1 — Line Start with ^ and m

Find lines that begin with start.

JavaScript
const text = "start here\nnot start\nstart again";

console.log(/^start/.test(text));    // true (first line)
console.log(/^start/m.test(text));   // true (lines 1 & 3)

console.log(text.match(/^start/gm)); // ["start", "start"]
Try It Yourself

How It Works

Without m, ^ only checks the string beginning. With m, each newline creates a new position where ^ can match.

📈 Practical Patterns

Line endings, extraction, and config parsing.

Example 2 — Line End with $ and m

Match lines that end with a specific word.

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

console.log(/end$/.test(text));    // false (string ends with "match")
console.log(/end$/m.test(text));  // true

console.log(text.match(/end$/gm).length); // 2
Try It Yourself

How It Works

With m, $ matches before each newline, not only at the final character of the string.

Example 3 — Extract Every Line with /^.+$/gm

Pull each non-empty line 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
Try It Yourself

How It Works

^ and $ with m bound each match to one line. The g flag collects every line. Alternative: text.split("\n").

Example 4 — Read the multiline Property

Check whether a RegExp was created with the m flag.

JavaScript
const withM = /^line/m;
const withoutM = /^line/;

console.log(withM.multiline);     // true
console.log(withoutM.multiline);  // false
console.log(withM.flags);         // "m"
Try It Yourself

How It Works

multiline mirrors the m flag at creation time, similar to global and ignoreCase.

Example 5 — Find Comment Lines in Config Text

Match every line that starts with #.

JavaScript
const config = "host=localhost\n# comment\nport=3000\n# another";

const comments = config.match(/^#.+$/gm);
console.log(comments);        // ["# comment", "# another"]
console.log(comments.length); // 2
Try It Yourself

How It Works

This pattern is a classic multiline use case. Without m, ^# would only match if the entire string started with #.

🚀 Common Use Cases

  • Config files — parse .env, INI, or hosts files line by line.
  • Log analysis — match lines starting with ERROR or WARN.
  • Input validation — ensure each line in a textarea meets a rule.
  • Comment stripping — find or remove # or // comment lines.
  • Blank line detection — locate empty lines with /^\s*$/gm.
  • Markdown/CSV prep — extract or transform individual lines before further parsing.

Important Considerations

  • Not dotAllm does not make . match newlines; use the s flag for that.
  • Combine with g — use gm when you need every line match, not just the first.
  • Line endings — Windows \r\n may leave \r at line ends; consider normalizing first.
  • Single-line stringsm has no effect if the string contains no newlines.
  • Flags are fixed — you cannot add m after creating the RegExp; build a new one.

🧠 How the m Modifier Changes Anchors

1

Scan string

The engine walks the input, including newline characters.

Read
2

Expand ^ and $

With m, ^ matches after \n; $ matches before \n.

Anchor
3

Match per line

Patterns like /^#/ can succeed on any line, not just line 1.

Match
4

Collect with g

Add g to gather matches from every line in the string.

📝 Notes

  • m affects ^ and $ only—not \b word boundaries.
  • See \n for the newline character matched at line breaks.
  • Previous modifier: i ignoreCase flag.
  • Next topic: $ end-of-input quantifier.
  • For string-wide anchors without line mode, omit m.
  • Flag order is free: gm equals mg.

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 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 Excellent

Bottom line: Safe everywhere. Use m when ^ or $ should apply to each line in multi-line text.

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.

💡 Best Practices

✅ Do

  • Add m when using ^ or $ on multi-line text
  • Use gm to match anchors on every line
  • 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

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
📈 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.

Continue to $ end quantifier

Learn how the $ anchor matches the end of input in regular expressions.

$ quantifier tutorial →

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.

6 people found this page helpful