Module 3 · Lesson 3 of 411/32 in the course~16 min
Module lessons (3/4)
The `m` flag: multiline
Lesson preview5 hands-on exercises readyRegex Course · Module 3 — Anchors and boundaries
With the m flag, ^ and $ anchor to the start/end of each line.
What you'll learn in this lesson
- Understand how the
m(multiline) flag transforms^and$from anchors for the entire string to anchors for each line delimited by\n. - Clearly distinguish the flag
m(effect on^and$) from the flags(dotAll, effect on point.). - Always combine
gandm(gm) when extracting or filtering multiline logs, configurations, or CSV line by line. - Avoid the mistake of expecting more matches by just using
mwithout the global flagg.
Overview
By default ^ and $ only check the absolute start and absolute end of the entire string. By activating the m (multiline) flag, both also anchor immediately after and immediately before each newline character \n, allowing you to treat a multiline string as an independent sequence of lines.
Sections covered in this lesson
- 1Mental model: orthogonality between
g,mands - 2Progressive examples
- 3Guided laboratory
- 4Check for understanding
Pro Lesson · Module 3 — Anchors and boundaries
Unlock "The `m` flag: multiline" and the full Regex Course curriculum
With the `m` flag, `^` and `$` anchor to the start/end of each line.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Regex Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.