JavaScript Regex m Flag (Multiline)
The m flag in JavaScript regular expressions allows the ^ and $ anchors to match the start and end of each line within a multi-line string, rather than just the
Introduction to Multiline Mode
The m flag in JavaScript regular expressions allows the ^ and $ anchors to match the start and end of each line within a multi-line string, rather than just the start and end of the entire string. This is particularly useful when working with multi-line text where you need to perform line-by-line pattern matching.
This page covers what the m flag changes, how it differs from default anchor behavior, how it combines with the g and i flags, and practical patterns for parsing line-oriented text such as logs.
Default Behavior vs. Multiline Mode
By default (without m), ^ matches only at the very start of the whole string and $ only at the very end. A line break (\n) inside the string is just an ordinary character — it does not create new anchor positions.
With the m flag, ^ also matches the position right after every \n, and $ also matches the position right before every \n (as well as the true string start/end). The match itself never includes the newline character — the anchors are zero-width positions.
Without m, only the abc at the very start of the string is anchored by ^. Adding m lets ^ also match after the line break, so both abc lines are found. (The g flag is what collects all matches rather than stopping at the first.)
The m Flag: Enabling Multiline Mode
When you use the m flag, the ^ and $ anchors match the positions before and after line breaks within the string.
Using the m Flag
In this example, the ^abc regex matches the start of each line that begins with "abc" due to the m flag. The g flag ensures all occurrences are matched. Without the m flag, it would only match "abc" at the very start of the string.
Example: Matching Multiple Lines
Here, the regex ^start matches the beginning of lines that start with "start" due to the combination of the m and g flags.
Practical Applications
Matching Lines in Multi-line Text
The m flag is particularly useful for matching specific patterns at the start or end of each line in a multi-line string.
This example shows how ^hello with the m flag matches "hello" at the start of each line.
Matching End of Lines in Multi-line Text
Similarly, you can use the m flag to match patterns at the end of each line.
Here, world$ with the m flag matches "world" at the end of each line.
Example: Extracting Specific Lines
This example demonstrates how to extract all lines that start with "Error:" from a log file using the m flag.
Combining Flags
You can combine the m flag with other flags to enhance your regular expressions. For instance, combining the m flag with the case-insensitive i flag allows you to perform case-insensitive multi-line matching.
Example: Case-Insensitive Multi-line Matching
In this example, the regex matches lines containing "hello" regardless of case, due to the combination of the m and i flags.
- The regex pattern
^.*hello.*$matches any line that contains "hello" regardless of case. - The
mflag ensures that each line in the multi-line string is treated individually. Hello world,hello world, andHELLO WORLDall match the pattern because they contain "hello" (case-insensitive) somewhere in the line.
Use the m flag in JavaScript regex to apply ^ and $ anchors to each line in a multi-line string, enhancing your ability to validate and manipulate line-by-line content.
Use the m Flag for Line-by-Line Validation
The m flag is ideal for scenarios where you need to validate or manipulate each line individually within a multi-line string.
Example: Validating a U.S. Phone Number
This regex ensures that the phone number is in the format (123) 456-7890. It uses ^ to assert the start and $ to assert the end, ensuring that the entire string matches the specified pattern for each line.
So, both "(123) 456-7890" and "(234) 567-8901" match the regex pattern because they follow the format of (xxx) xxx-xxxx, which the regex is designed to match.
Common Pitfalls
maffects only^and$, not.— The dot still refuses to match a newline. If you need.to cross line breaks too, use the separates(dotAll) flag. Themflag changes where lines begin and end; thesflag changes what.matches.mis not the same as matching whole words. To match at the edges of a word rather than a line, use the\bword boundary instead of^/$.- Without
g,match()returns only the first hit. Themflag enables extra anchor positions, but you still needgto collect every line. Combine them asgm.
Conclusion
The m flag in JavaScript regular expressions extends the functionality of the ^ and $ anchors to match the start and end of each line in a multi-line string. This capability is invaluable for processing multi-line text data, enabling precise line-by-line pattern matching and validation.