A regex newline pattern matches line breaks inside a string or file. The tricky part is that a line break is not always one character: Unix and modern macOS text usually uses \n, old Mac text used \r, and Windows text commonly uses \r\n. A pattern that works on one pasted sample can fail on logs, CSV files, email text, or copied terminal output if it assumes the wrong newline style.
This guide covers \n, \r, \r\n, dotAll / s, multiline / m, and the portable [\s\S] trick for matching across lines. You can test every example in RegEx Visualizer, a browser regex tester from Tools Viewer with JavaScript, PCRE2 WASM, and Go RE2 WASM dialects, Compare, Replace, and presets. For core testing, your regex pattern and sample text stay on-device.
Quick answer: match a line break
If you need to match a line break in JavaScript and most common regex engines, start with \r?\n. It matches a Unix line feed and a Windows carriage-return plus line-feed pair. If you also need to accept isolated carriage returns, use \r\n?|\n. If your engine supports \R, as PCRE2 does, that can represent a broader newline sequence, but it is not a JavaScript feature.
\nmeans line feed, often called LF.\rmeans carriage return, often called CR.\r\nmeans CRLF, common in Windows files and many network formats.\r?\nmeans optional CR followed by LF, so it matches LF and CRLF.\r\n?|\nmatches CRLF, lone CR, or LF.
The right choice depends on where the text comes from. Browser textareas often normalize line endings before your JavaScript sees them. Files, copied log excerpts, and email sources may preserve mixed endings. When you are unsure, paste real examples into the tester and use visible matches instead of guessing from the operating system.
Why dot does not match newline
In most regex engines, . means any character except a line terminator. That is why a pattern like <div>.*</div> may work for one-line HTML but fail as soon as the opening and closing tags are separated by line breaks. The dot is not broken; it is following the default rule.
To match any character including newline, choose one of these approaches:
- Enable the
sflag, also called dotAll, so.can match line breaks. - Use
[\s\S], a class that means whitespace or non-whitespace, which covers every character. - In some flavors, use an engine-specific mode or token, but check that your production engine supports it.
Example: <!--([\s\S]*?)--> finds an HTML comment that spans lines. The *? quantifier is non-greedy, so it stops at the next closing marker instead of racing to the last one. In RegEx Visualizer, add two comments to the sample text and confirm the pattern returns two matches, not one giant match.
DotAll s flag vs multiline m flag
The s and m flags solve different newline problems. The s flag changes dot behavior: . can match line breaks. The m flag changes anchor behavior: ^ and $ can match the start and end of each line, not only the start and end of the whole string.
For example, ^ERROR with m andg finds every line that begins with ERROR. Without m, it only matches if the entire string begins with that word. By contrast, ERROR.*END needs s if ERROR and END may be on different lines. Many real patterns use both flags: m to locate line boundaries and s to let a section body span lines.
Use anchors for line-by-line matching
Newline questions often involve anchors. Use ^.+$ with m to match non-empty lines, or ^\s*$ with m to find blank or whitespace-only lines. Remember that \s includes newlines, so be careful when a line pattern contains \s*. If that token can consume the line break, the match may stretch farther than you expected.
When the goal is to capture whole lines, prefer patterns that exclude line breaks explicitly, such as [^\r\n]+. That character class says one or more characters that are not carriage return and not line feed. It is often clearer than relying on dot rules, especially when a teammate may later toggle the s flag.
Splitting, replacing, and normalizing newlines
Replace mode is useful for seeing exactly what a newline regex will do. To normalize mixed line endings to LF, match \r\n?|\n and replace with the newline string your target language expects. To remove blank lines, start with ^\s*\r?\n using g and m, then test with lines that contain spaces and tabs.
If you want to join wrapped lines without destroying paragraph breaks, do not blindly replace every \r?\n with a space. First decide what counts as a paragraph boundary. Some workflows replace single line breaks with a space but leave two or more line breaks intact. A starting pattern for double breaks is (?:\r?\n){2,}, but real text often includes blank lines with spaces, so you may need (?:\r?\n[ \t]*){2,}.
Engine notes: JavaScript, PCRE2, and RE2
JavaScript supports the s and m flags in modern browsers. PCRE2 supports many newline-related features, including\R for newline sequences. Go RE2 is designed for predictable performance and does not support every advanced PCRE2 feature. RegEx Visualizer lets you switch dialects or use Compare to see how JavaScript, PCRE2 WASM, and Go RE2 WASM handle the same pattern and sample text.
The tester shows regex behavior, but host language strings still matter. In JavaScript source code, a regex literal like /\r?\n/g is not written the same way as a string passed to new RegExp. JSON, shell commands, and configuration files add their own escaping rules. Confirm the pattern in the visualizer, then copy it into the target language with the right string quoting.
Whitespace overlap
\s already includes newline characters. That is helpful for matching any spacing but risky when you only meant a literal space. If you are cleaning text and want spaces or tabs without crossing lines, use [ \t]. For a deeper breakdown, see regex space and whitespace.
The opposite also matters: if a pattern uses \S+, it stops at newlines because newlines are whitespace. That makes \S+ useful for tokens, but not for paragraphs. Use [\s\S]+? or dotAll when a match must cross line boundaries.
Try it step by step
- Open RegEx Visualizer.
- Paste a multi-line sample with a blank line, an indented line, and text that should not be matched.
- Try
\n, then\r?\n, then\r\n?|\n. - Toggle
mand test^/$line anchors. - Toggle
sand compare.with[\s\S]. - Open Replace to preview newline normalization before changing real text.
- Use Compare when a pattern must behave the same in JavaScript, PCRE2, and Go RE2.
FAQ
How do I match a new line in regex?
Use \n for LF, \r\n for Windows CRLF, or \r?\n to accept LF and CRLF. Use \r\n?|\n if lone carriage returns must also count.
Why does the dot not match my line break?
By default, . stops at line terminators. Enable the s dotAll flag or use [\s\S] when the match needs to span lines.
What is the difference between m and s?
m changes anchors so ^ and $ can match line boundaries. s changes dot behavior so . can match newlines.
Does \s match newlines?
Yes. \s includes newline characters. Use [ \t] if you want horizontal whitespace only.
When should I use [\s\S]?
Use [\s\S] when you need a portable any-character pattern that spans newlines, especially where dotAll is unavailable or not enabled.
Can I compare newline regex behavior across engines?
Yes. RegEx Visualizer includes JavaScript, PCRE2 WASM, and Go RE2 WASM dialects plus a Compare tab for checking the same sample across engines.
