Quick answer: To match a space in regex, type the space character directly (or use [ ]). To match any whitespace, including tabs and line breaks, use \s. To match spaces and tabs but not line breaks, use [ \t]. Add + for one or more, or * for zero or more.
A regex space pattern can mean several different things: one literal space, a run of blanks, any whitespace including tabs and line breaks, or a rule that allows spaces inside a longer value. The safest pattern depends on which of those jobs you are actually doing. A literal space is narrow, \s is broad, and a character class such as [ \t] sits in between when you want horizontal whitespace but not newlines.
This guide explains the practical choices for regex whitespace, regex allow spaces, and \s vs literal space. You can test every example in RegEx Visualizer, a browser-based regex tester from Tools Viewer that matches with JavaScript RegExp and includes Replace mode and presets such as Whitespace runs. Your pattern and sample text stay on your device in the browser.
Regex Whitespace Cheat Sheet
| Pattern | Matches | Tabs? | Line breaks? |
|---|---|---|---|
| Empty space or [ ] | One ordinary space (U+0020) | No | No |
| [ ]+ | One or more ordinary spaces | No | No |
| [ \t]+ | Spaces and tabs in a row | Yes | No |
| [^\S\r\n]+ | Any whitespace except line breaks | Yes | No |
| \s | One whitespace character | Yes | Yes |
| \s+ | One or more whitespace characters | Yes | Yes |
| \s* | Optional whitespace (can match nothing) | Yes | Yes |
| \S+ | A run of non-whitespace characters | n/a | n/a |
That distinction is the source of many bugs. A form field that accepts first and last names separated by spaces does not automatically need to accept newlines. A log parser that splits on any spacing may need tabs as well as spaces. A cleanup rule that collapses all whitespace may intentionally turn paragraphs into one line, while a rule that collapses only spaces leaves line breaks alone.
What Does \s Match in Regex?
In JavaScript, \s matches a set of characters, not just the space bar:
- The ordinary space (U+0020)
- Tab (
\t), line feed (\n), carriage return (\r), vertical tab, and form feed - The non-breaking space (U+00A0)
- Other Unicode space separators, the line and paragraph separators, and the byte order mark
It does not match the zero-width space (U+200B). Other regex engines differ in the details, so re-test in your target language if the exact set matters.
The non-breaking space is worth remembering. Text copied from web pages, PDFs, and word processors often contains U+00A0 instead of a normal space. A pattern with a literal space will silently skip it, while \s will match it. If a pattern "works on my sample but not on pasted text," this is a common cause.
How to Match a Literal Space
The simplest regex for one literal space is a space typed directly into the pattern. It is easy to miss when reading code, so many teams write [ ] to make the intent visible. Both forms match the same ordinary space character.
To match exactly one space between two words:
^[A-Za-z]+ [A-Za-z]+$This pattern accepts Jane Doe but rejects a value with two spaces between the names, a tab between the names, or a line break between them. If your requirement is one or more spaces, use [ ]+. If it is any horizontal spacing, use [ \t]+. If it is any whitespace at all, including line breaks, use \s+.
How to Allow Spaces in a Regex
To allow spaces inside a value, add the space to a character class, or place a whitespace token between stricter pieces. The first approach suits simple inputs. The second gives you control over where spaces may appear.
^[A-Za-z ]+$allows ASCII letters and spaces anywhere in the value.^[A-Za-z]+(?: [A-Za-z]+)*$allows words separated by single spaces, with no leading or trailing space.^[A-Za-z]+(?:[ \t]+[A-Za-z]+)*$allows spaces or tabs between words, but not newlines.^\s*[A-Za-z]+(?:\s+[A-Za-z]+)*\s*$allows flexible surrounding whitespace when your program trims later.
Avoid using one broad pattern for every text field. A product title may reasonably accept digits, punctuation, and repeated spaces. A person's name may need hyphens and apostrophes. A slug or code usually should not allow spaces at all. Start from the data rules, then choose the smallest pattern that matches them.
Match Spaces and Tabs but Not Newlines
A common requirement is horizontal whitespace only: match spaces and tabs, but stop at the end of a line. You have two reliable options:
[ \t]+is the most readable. It covers ordinary spaces and tabs only.[^\S\r\n]+reads as "not a non-whitespace character, and not\ror\n." It matches every whitespace character except line breaks, including the non-breaking space, so it is the more complete choice for messy pasted text.
Use the first when you control the input, and the second when the text comes from copy and paste.
Collapse Whitespace Runs With Replace Mode
Cleanup tasks often need a regex that finds repeated spaces or mixed whitespace. In RegEx Visualizer, load the Whitespace runs preset, paste messy text, and open Replace mode. Then compare these patterns:
[ ]{2,}finds two or more ordinary spaces.[ \t]+finds horizontal runs.\s+finds every whitespace run, including line breaks.
For one-line text, a common replacement is a single space. In JavaScript:
const clean = text.replace(/[ \t]{2,}/g, " ");Be careful with \s+. Replacing it globally can turn a multi-line address, stack trace, or Markdown list into a single line. A safer first pass is usually [ \t]{2,} replaced with one space, followed by a separate rule for blank lines if needed.
Trim Leading and Trailing Whitespace
Trim-style patterns are useful for analysis and replace previews:
^\s+matches leading whitespace.\s+$matches trailing whitespace.^\s+|\s+$matches either side (use thegflag).
With the m flag, ^ and $ apply to each line, so ^[ \t]+ can find indentation across a pasted file. In JavaScript code, the built-in trim() method is usually simpler than a regex when you only need to strip both ends of a string.
Optional Spaces Around Separators
Optional spaces belong around separators, not usually around the entire value. For example, \s*,\s* suits a comma-separated list where users may type spaces around the commas. The same idea works for colons, equals signs, and pipes.
Be careful with \s* inside repeated groups. Because it can match nothing, it can make a pattern appear to match everywhere, or slow it down on long input. If at least one separator is required, use \s+ instead.
JavaScript Matching and Other Languages
RegEx Visualizer matches with JavaScript RegExp, and the whitespace examples on this page use syntax that is widely familiar. When a rule later moves into another host language, re-test it there. Export snippets are copy-paste helpers for string quoting and API shape, not substitutes for that language's regex engine.
A pattern copied into a JavaScript string, JSON file, shell command, or database migration may need another layer of escaping. For example, \s written inside a normal JavaScript string must become "\\s". Confirm match behavior in the visualizer, then adapt the quoting for your target.
Common Mistakes With Regex Spaces
- Using
\swhen a literal space was intended. This can accidentally match newlines and tabs. - Forgetting the
gflag. Without it, only the first whitespace run is matched. - Writing
\s*where a separator is required.\s*also matches zero characters. - Assuming
\wincludes spaces. It does not. In common usage it covers letters, digits, and underscore. - Missing the non-breaking space. A literal-space pattern will not match U+00A0.
- Testing only clean examples. Add tabs, double spaces, leading spaces, trailing spaces, and line breaks before trusting a pattern.
Try It Step by Step
- Open RegEx Visualizer.
- Load the Whitespace runs preset, or paste
\s+into the pattern field. - Add a sample with single spaces, repeated spaces, a tab, and at least one line break.
- Toggle
gto see every run, then compare a literal space,[ \t]+, and\s+. - Open Replace and preview collapsing spaces before using the rule on production text.
Conclusion
Matching spaces in regex comes down to choosing the right width. Use a literal space when only U+0020 is acceptable, [ \t] when horizontal whitespace is fine but line breaks are not, and \s when any whitespace should match. Anchor patterns with ^ and $ for validation, add + or * deliberately, and test with tabs, repeated spaces, and pasted text before you ship.
Test your patterns live in RegEx Visualizer, then move on to matching newlines and line breaks in regex to handle multi-line text.
FAQs
How do I match a space in regex?
[ ] to make it visible. Both match one ordinary space (U+0020).What is the difference between \s and a literal space in regex?
\s matches any whitespace character, including tabs, line breaks, and the non-breaking space.How do I allow spaces in a regex?
^[A-Za-z ]+$, or separate words with a controlled pattern such as ^[A-Za-z]+(?: [A-Za-z]+)*$ to prevent leading, trailing, or repeated spaces.How do I match multiple spaces in regex?
[ ]+ for one or more spaces, or [ ]{2,} for two or more. Use \s+ if tabs and line breaks should count as well.How do I match spaces and tabs but not newlines?
[ \t]+. For a more complete version that also covers non-breaking spaces, use [^\S\r\n]+.Does \s match a newline?
\s matches line feeds and carriage returns as well as spaces and tabs. To exclude them, use [ \t] or [^\S\r\n].How do I remove extra spaces with regex?
[ \t]{2,} and replace it with a single space. Use ^\s+|\s+$ with the g flag to trim both ends of a string.Why does my regex not match a space I can see?
\s will.
