/s (DotAll (single line))
What is /s (DotAll (single line))?
/s (DotAll (single line)): Makes . match newline characters too, not just non-newline characters. The s flag, often called dotall or single-line mode, makes . match newline characters too. Normally . matches any character except a line break, so a pattern like /<div>.*<\/div>/ stops at the end of the first line. With s, the same pattern can span several lines. That is what you need when extracting a block from a multi-line message, an email body or a log entry that wraps.
Frequently asked questions about /s (DotAll (single line))
What should you keep in mind with /s (DotAll (single line))?
Combine it with care. .* with dotall is greedy and can swallow far more than you expect, all the way to the last closing tag in a large file. Use the lazy form .*? or a more specific class. For real HTML or XML, a parser is safer than any regex. Do not confuse this flag with m, which changes how ^ and $ behave.
At a glance
| Category | Regex flags |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |