JavaScript RegExp multiline Property

Beginner
5 min read
Updated: Nov 2026
3 examples
RegExp

Definition

In JavaScript, regex.multiline is a read-only boolean. It is true when the RegExp was created with the m flag.

With m, the anchors ^ and $ match the start and end of each line, not only the whole string. Without m, they match only the start and end of the entire text.

Syntax

Create the regex with m, then read .multiline.

JavaScript
const a = /end$/;
const b = /end$/m;
const c = new RegExp("end$", "m");

console.log(a.multiline); // false
console.log(b.multiline); // true
console.log(c.multiline); // true
CallWhat it does
/end$/Anchors apply to the whole string. multiline is false.
/end$/mAnchors apply per line. multiline is true.
new RegExp("end$", "m")Same as the literal with m.

Remember this at a glance:

Remember
 /hello$/.multiline     → false
 /hello$/m.multiline    → true

without m:  ^ and $ → whole string only
with m:     ^ and $ → start/end of every line

flags are fixed at creation — cannot set .multiline = true

What You Get

NameDescription
regex.multilineRead-only. true if the m flag was set when the RegExp was created.
m flagSet at creation with /…/m or new RegExp(pattern, "m").
^ and $With m, match the start and end of each line (split by newline).

Return Value

ValueDescription
trueThe RegExp has the m (multiline) flag.
falseThe RegExp does not have the m flag.

JavaScript Examples

Example 1 — Read multiline

See how the m flag sets the property.

JavaScript
const a = /end$/;
const b = /end$/m;
const c = new RegExp("^start", "m");

console.log(a.multiline);
console.log(b.multiline);
console.log(c.multiline);
console.log(c.flags);
Try It Yourself

How It Works

  1. /end$/ has no m, so multiline is false.
  2. /end$/m sets multiline to true.
  3. The constructor with "m" does the same.
  4. flags reports the flag string "m".

Example 2 — Match the end of each line

Without m, $ only sees the end of the whole string.

JavaScript
const text = "one\ntwo\nthree";

console.log(/two$/.test(text));
console.log(/two$/m.test(text));
console.log(/^three/m.test(text));
Try It Yourself

How It Works

  1. /two$/ fails because the string ends with three, not two.
  2. With m, $ matches the end of the line that contains two.
  3. ^three with m matches the last line.

Example 3 — Replace at the start of every line

Use ^ with m and g to edit each line.

JavaScript
const text = "apple\nbanana\ncherry";
const out = text.replace(/^/gm, "> ");

console.log(out);
Try It Yourself

How It Works

  1. ^ with m means “start of each line”.
  2. The g flag replaces every match, not only the first.
  3. Each line gets a "> " prefix.

Frequently Asked Questions

It is true when the regex was created with the m flag. Then ^ and $ match the start and end of each line, not only the whole string.
Add m when you create the regex: /pattern/m or new RegExp("pattern", "m"). You cannot change multiline later on the same object.
No. The m flag only affects ^ and $. The s (dotAll) flag controls whether . matches newlines.
Lines are split by newline characters such as \n. Each segment between newlines is a line for ^ and $ when m is set.
Yes. /pattern/gm is common when you want ^ or $ on every line and every match.
They describe the same flag. This page focuses on the .multiline property; the m modifier page focuses on writing the flag.

Next: dotAll

Let the . wildcard match newline characters with the s flag.

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

9 people found this page helpful