Lesson guide
Learning objectives
- Explain the main purpose of Multiline mode of anchors ^ $, flag "m" in JavaScript.
- Identify the syntax, APIs, or concepts introduced in this lesson.
- Use the examples to predict how JavaScript will behave before you run similar code.
- Connect this topic to nearby lessons in Regular expressions.
Real-world context
This lesson matters when you need to recognize where Multiline mode of anchors ^ $, flag "m" fits into real JavaScript programs. The multiline mode is enabled by the flag .
Key ideas
- Multiline mode of anchors ^ $, flag "m" is part of the Regular expressions chapter, so it builds on the surrounding concepts rather than standing alone.
- Read each code example in two passes: first for the result, then for the rule that explains the result.
- When a section compares similar features, focus on the condition that makes you choose one feature over another.
Key terms
- Multiline mode of anchors ^ $, flag "m"
- Multiline
- anchors
- flag
- Regular expressions
The multiline mode is enabled by the flag m.
It only affects the behavior of ^ and $.
In the multiline mode they match not only at the beginning and the end of the string, but also at start/end of line.
Searching at line start ^
In the example below the text has multiple lines. The pattern /^\d/gm takes a digit from the beginning of each line:
let str = `1st place: Winnie 2nd place: Piglet 3rd place: Eeyore`; console.log( str.match(/^\d/gm) ); // 1, 2, 3
Without the flag m only the first digit is matched:
let str = `1st place: Winnie 2nd place: Piglet 3rd place: Eeyore`; console.log( str.match(/^\d/g) ); // 1
That’s because by default a caret ^ only matches at the beginning of the text, and in the multiline mode – at the start of any line.
Please note:
“Start of a line” formally means “immediately after a line break”: the test ^ in multiline mode matches at all positions preceded by a newline character \n.
And at the text start.
Searching at line end $
The dollar sign $ behaves similarly.
The regular expression \d$ finds the last digit in every line
let str = `Winnie: 1 Piglet: 2 Eeyore: 3`; console.log( str.match(/\d$/gm) ); // 1,2,3
Without the flag m, the dollar $ would only match the end of the whole text, so only the very last digit would be found.
Please note:
“End of a line” formally means “immediately before a line break”: the test $ in multiline mode matches at all positions succeeded by a newline character \n.
And at the text end.
Searching for \n instead of ^ $
To find a newline, we can use not only anchors ^ and $, but also the newline character \n.
What’s the difference? Let’s see an example.
Here we search for \d\n instead of \d$:
let str = `Winnie: 1 Piglet: 2 Eeyore: 3`; console.log( str.match(/\d\n/g) ); // 1\n,2\n
As we can see, there are 2 matches instead of 3.
That’s because there’s no newline after 3 (there’s text end though, so it matches $).
Another difference: now every match includes a newline character \n. Unlike the anchors ^``$, that only test the condition (start/end of a line), \n is a character, so it becomes a part of the result.
So, a \n in the pattern is used when we need newline characters in the result, while anchors are used to find something at the beginning/end of a line.
Common mistakes
- Skipping the small examples and then missing the exact rule that Multiline mode of anchors ^ $, flag "m" depends on.
- Copying code without changing one value at a time to see which part controls the result.
- Treating similar-looking syntax or APIs as interchangeable before checking their edge cases.
Summary
- Multiline mode of anchors ^ $, flag "m" gives you one more piece of the JavaScript mental model.
- The examples in this lesson are the quickest way to check whether the rule is clear.
- Revisit the key terms when you meet the same idea in later chapters.
Predict
Before running this multiline-anchor block, predict the five output lines. It compares ^ and $ with and without flag m, then shows why matching \n is not the same as using an end-of-line anchor.
Reveal explanation
With flag m, ^ matches the start of every line and $ matches the end of every line. Without m, ^ only checks the beginning of the whole string. \d\n matches a digit plus an actual newline character, so it misses the final 3 because there is no newline after it, and the matched text has length 2.
Try it
Add a trailing newline after Eeyore: 3, then predict newline-count. Then remove m from \d$/gm and predict line-end-m.
Practice
- Rewrite one example from this lesson without looking at the original, then run it and compare the result.
- Change one input, operator, method call, or option in a code sample and predict what will happen before running it.
- Explain Multiline mode of anchors ^ $, flag "m" in your own words as if you were reviewing it with another learner.
Keep learning
Continue with Word boundary: \b when you are ready for the next lesson.