JavaScript RegExp m Modifier

Definition and Usage

"m" modifier specifies multiline matching.

It only affects the beginning. ^ and the end. $ behavior.

^ specifies the match at the beginning of the string.

$ specifies the match at the end of the string.

After setting "m",^ and $ Also matches the beginning and end of each line.

Instance

Perform a multiline search for "is" at the beginning of each line in the string:

let text = `Is this
all there
is`
let pattern = /^is/m;

Try it yourself

Hint 1

The "m" modifier is case-sensitive rather than global.

To perform a global, case-insensitive search, use "m" with "g" and "i" together.

Example 1

Perform a global multiline search for "is" at the beginning of each string line:

let text = `Is this
all there
is`
let pattern = /^is/gm;

Try it yourself

Example 2

Perform a global, case-insensitive multiline search for "is" at the beginning of each string line:

let text = `Is this
all there
is`
let pattern = /^is/gmi;

Try it yourself

Example 3

Perform a global multiline search for "is" at the end of each string line:

let text = `Is this
all there
is`
let text = "Is\nthis\nhis\n?";
let pattern = /is$/gm;

Try it yourself

Hint 2

Can be used multiline Property check if set m Modifiers.

Check if the "m" modifier is set:
let pattern = /W3S/gi;
let result = pattern.multiline;

Try it yourself

Syntax

new RegExp("regexp"m")

or abbreviated as:

/regexp/m

Browser support

/regexp/m is ECMAScript3 (ES3) feature.

All browsers support ES3 (JavaScript 1999):

Chrome IE Edge Firefox Safari Opera
Supports Supports Supports Supports Supports Supports

Regular expression search methods

In JavaScript, regular expression text search can be completed using different methods.

UsagePattern (pattern)As regular expressions, these are the most commonly used methods:

Example Description
text.match(pattern) string method match()
text.search(pattern) string method search()
pattern.exec(text) RexExp method exec()
pattern.test(text) RexExp method test()