Regex to Match Whitespace and Newlines, Explained Without the Usual Confusion
Regex to match whitespace and newlines explained simply: the pattern \s matches one whitespace character, including spaces, tabs, and newlines, and \s+ matches a run of them. Most of the confusion comes from the cases where you want whitespace but not newlines, and those need a different pattern entirely.
Whitespace is the most boring character class in regex, and the one that wastes the most debugging time. It wastes time because it is invisible. You stare at two strings that look identical, one matches and one does not, and the difference is a tab where you expected a space, or a carriage return hiding at the end of a line copied from Windows. The pattern did not fail. Your eyes did.
So let us name the invisible things first, then build the patterns.
What \s actually matches
In most engines, \s matches a space, a tab (\t), a newline (\n), a carriage return (\r), a form feed (\f), and a vertical tab (\v). Think of it as the character class [ \t\n\r\f\v], and in Unicode aware mode it can include more, like a non-breaking space. Its opposite, \S, matches any single character that is not whitespace. That pair covers most jobs: \s+ collapses a messy run of spaces and line breaks into one gap, \S+ grabs a word, and splitting a string on \s+ breaks it into tokens regardless of whether the gaps are spaces, tabs, or newlines.
A worked example makes the behavior concrete. Take the string "hello world" with three spaces between the words. The pattern hello\s+world matches it, because \s+ eats all three spaces. Take "hello\nworld" with a newline instead, and the same pattern still matches, because \n is whitespace too. That is a feature when you are normalizing text, and a bug when you are parsing line by line and a pattern quietly jumps across a line boundary you meant to respect.
When you want whitespace but not newlines
The common fix is to stop using \s and name exactly what you mean. A literal space plus a tab, written as [ \t] or [ \t]+ for a run, matches horizontal gaps only and never crosses a line. That is the pattern for cleaning up spacing inside a line, aligning columns, or parsing a single line format like a log entry where a newline means a new record.
The clever looking alternative you will see in Perl and other engines is [^\S\r\n], which reads as "a character that is not a non-whitespace character, and not a carriage return or newline." It works, and it also reads like a riddle. Prefer [ \t] unless your team already uses the negated form, because the next person to read the pattern will understand [ \t] in one glance and will misread the riddle at least once. Regex that needs a comment to be understood is regex that will be broken by the next edit.
Two more flags change newline behavior, and they are worth knowing separately because people conflate them. The multiline flag, usually m, changes what ^ and $ mean: with it, they match at the start and end of each line, not just the whole string. The dotAll flag, usually s, changes what the dot matches: with it, the dot matches newlines too, so .* can cross lines. Neither flag changes what \s matches. If your pattern is crossing lines when it should not, the fix is usually the character class, not a flag.
The three mistakes I keep seeing
First, using a plain space in a pattern when the input might contain tabs. Data copied from spreadsheets, PDFs, and web forms arrives with tabs and non-breaking spaces hiding in it, and a pattern with a literal space will miss them while \s catches them. If the input is untrusted, \s is the safer default, and you narrow it only when you have a reason.
Second, trimming with the wrong tool. The pattern ^\s+|\s+$ strips leading and trailing whitespace, and it works, but in most languages a built in trim method does the same job faster and clearer. Use regex trimming only when you need to trim a specific subset, like spaces but not newlines at the edges of a block of text. Otherwise, call trim and move on.
Third, forgetting the carriage return. A file with Windows line endings ends each line with \r\n, not just \n. A pattern that splits on \n alone leaves a \r clinging to the end of every line, and that invisible character then breaks the next comparison, the next length check, the next equality test. Splitting on \r?\n, or normalizing line endings before parsing, kills a whole class of ghost bugs. I once spent an afternoon on a "string mismatch" that was a single \r. I do not want that afternoon back, but I do remember the lesson, which is why it is in this article.
Frequently asked questions
What does \s match in regex?
\s matches one whitespace character: a space, tab, newline, carriage return, form feed, or vertical tab, and more in Unicode aware mode. \S matches any single character that is not whitespace.
How do I match whitespace but not newlines?
Use [ \t] or [ \t]+ instead of \s. That matches spaces and tabs only and never crosses a line boundary, which is what you want when parsing one line at a time.
What is the difference between \s and \s+?
\s matches exactly one whitespace character. \s+ matches one or more consecutive whitespace characters, so it collapses a run of spaces, tabs, or line breaks into a single match.
Does the multiline flag change what \s matches?
No. The multiline flag changes what ^ and $ mean, letting them match at line boundaries. The dotAll flag changes whether the dot matches newlines. Neither changes the \s character class itself.
Why does my string comparison fail when the text looks identical?
Often a hidden carriage return from Windows line endings, \r\n, is clinging to the end of a line. Split on \r?\n or normalize line endings before parsing to remove it.
Related reading: Regex Word Boundaries, Explained · Regex Greedy vs Lazy Matching · The Regex Multiline Flag, Explained · Regex Catastrophic Backtracking: What It Is and How to Avoid It