/m (Multiline)
What is /m (Multiline)?
/m (Multiline): Makes ^ and $ match the start and end of each line, not just the whole string. The m flag, short for multiline, changes what ^ and $ mean. Normally they match the start and end of the whole string. With m, they match the start and end of every line.
Frequently asked questions about /m (Multiline)
How is /m (Multiline) used in practice?
So /^error/m finds lines that begin with error anywhere in a log file. Without the flag it would only check the first line. This is the flag you want when you process text that contains many lines, such as logs, config files or CSV exports.
What should you keep in mind with /m (Multiline)?
People often confuse it with the s flag, which makes . match newline characters. They solve different problems and can be combined. If a pattern works on a single line in your test but finds nothing in a real file, a missing m is a likely cause. If you need the true start or end of the whole string while m is on, use \A and \z.
At a glance
| Category | Regex flags |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |