Skip to main content
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 flag s (dotAll, effect on point .).
  • Always combine g and m (gm) when extracting or filtering multiline logs, configurations, or CSV line by line.
  • Avoid the mistake of expecting more matches by just using m without the global flag g.

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

  1. 1Mental model: orthogonality between g, m and s
  2. 2Progressive examples
  3. 3Guided laboratory
  4. 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.