Tools Viewer

How to Match Spaces and Whitespace in Regex

Published on August 1, 2026 by Tools Viewer

How to Match Spaces and Whitespace in Regex
How to Match Spaces and Whitespace in Regex

Aregex spacepattern can mean several different things: one literal space character, 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.{String.raw`\s`}is broad. A character class such as{String.raw`[ \t]`}sits between them when you want horizontal whitespace but not newlines.

This explains the practical choices forregex whitespace,regex allow spaces, and{String.raw`\s`} vs literal space. You can test the examples inRegEx Visualizer, a browser regex tester from Tools Viewer that matches with JavaScript RegExp, plus Replace mode and presets including Whitespace runs. For core testing, your pattern and sample text stay on-device in the browser.

Quick answer: space vs whitespace

Use a literal space when only the normal U+0020 space should match. Use{String.raw`\s`}when tabs, newlines, carriage returns, form feeds, and other whitespace are acceptable. Use{String.raw`[ \t]`}or{String.raw`[ \t]+`}when you want spaces and tabs on a single line but must reject a line break.

  • {String.raw` `}as a typed space matches one literal space.
  • {String.raw`\s`}matches one whitespace character.
  • {String.raw`\s+`}matches one or more whitespace characters in a row.
  • {String.raw`\s*`}matches optional whitespace, including the empty string.
  • {String.raw`\S+`}matches a run of non-whitespace characters.

That distinction is the source of many bugs. A form field that should allow first and last names separated by spaces does not automatically need to accept newlines. A log parser that should split on any spacing may need tabs as well as spaces. A text cleanup rule that collapses all whitespace may intentionally turn paragraphs into one line, while a rule that collapses only spaces should leave line breaks alone.

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 teams sometimes use{String.raw`[ ]`}to make the intent visible. Both forms match the same ordinary space character. To match exactly one space between two words, write the space literally:

regex
^[A-Za-z]+ [A-Za-z]+$

This pattern acceptsJane Doebut rejectsJane Doe,Jane\tDoe, and a value with a line break between the names. If your real requirement is one or more spaces, use{String.raw`[ ]+`}. If the requirement is any horizontal spacing, use{String.raw`[ \t]+`}. If the requirement is any whitespace at all, including line breaks, use{String.raw`\s+`}.

How to allow spaces in a regex

Toallow spacesinside a value, add the space to the character class or place a whitespace token between stricter pieces. The first approach is useful for simple inputs; the second is better when you want to control where spaces can appear.

  • {String.raw`^[A-Za-z ]+$`}allows ASCII letters and spaces anywhere in the value.
  • {String.raw`^[A-Za-z]+(?: [A-Za-z]+)*$`}allows words separated by single spaces, with no leading or trailing space.
  • {String.raw`^[A-Za-z]+(?:[ \t]+[A-Za-z]+)*$`}allows spaces or tabs between words, but not newlines.
  • {String.raw`^\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 name might need hyphens and apostrophes, though this article escapes apostrophes in visible copy as requested. A slug or code usually should not allow spaces at all. Start from the data contract, then choose the smallest pattern that matches it.

Whitespace runs and Replace mode

Cleanup tasks often ask for a regex that finds repeated spaces or mixed whitespace. In RegEx Visualizer, load the Whitespace runs preset, paste messy text, and open Replace mode. Use{String.raw`[ ]{2,}`}to find two or more ordinary spaces. Use{String.raw`[ \t]+`}to find horizontal runs. Use{String.raw`\s+`}to find every whitespace run, including line breaks.

For collapsing spacing inside one-line text, a common replacement is a single space. For preserving paragraph breaks, do not replace{String.raw`\s+`}globally without checking the output. That pattern can turn a multi-line address, stack trace, or Markdown list into one line. A safer first pass is often{String.raw`[ \t]{2,}`}replaced with one space, followed by a separate rule for blank lines if needed.

Leading, trailing, and optional spaces

Trim-style regexes are useful for analysis and replace previews. Use{String.raw`^\s+`}for leading whitespace,{String.raw`\s+$`}for trailing whitespace, or{String.raw`^\s+|\s+$`}to match either side. With themflag,^and$apply to each line, so a pattern such as{String.raw`^[ \t]+`}can find indentation across a pasted file.

Optional spaces belong around separators, not usually around the entire value. For example,{String.raw`\s*,\s*`}is useful for a comma-separated list where users may type spaces around commas. The same idea works for colons, equals signs, and pipes. Be careful with{String.raw`\s*`}in repeated patterns, because it can match nothing and make a pattern feel as if it is matching everywhere.

JavaScript matching and export snippets

RegEx Visualizer matches with JavaScript RegExp. The basic space and whitespace examples on this page use syntax that is widely familiar, but the tool itself does not run alternate match engines. When a validation rule later moves into another host language, re-test there — export snippets are copy-paste helpers for string quoting and API shape, not substitutes for that host's regex runtime.

A pattern copied into a JavaScript string, JSON file, shell command, or database migration may need another layer of escaping. Use the visualizer to confirm match behavior in the browser, then adapt quoting for the target language.

Common mistakes with regex spaces

  • Using{String.raw`\s`}when a literal space was intended, which can accidentally match newlines.
  • Forgetting thegflag when you want every whitespace run, not just the first.
  • Writing{String.raw`\s*`}where at least one separator is required;{String.raw`\s*`}also matches zero characters.
  • Assuming{String.raw`\w`}includes spaces. It does not; in common ASCII-style usage it covers letters, digits, and underscore.
  • Testing only clean examples. Add tabs, double spaces, leading spaces, trailing spaces, and line breaks before trusting the pattern.

Try it step by step

  1. OpenRegEx Visualizer.
  2. Load the Whitespace runs preset, or paste{String.raw`\s+`}into the pattern field.
  3. Add a sample with single spaces, repeated spaces, a tab, and at least one line break.
  4. Togglegto see every run, then compare a literal space,{String.raw`[ \t]+`}, and{String.raw`\s+`}.
  5. Open Replace and preview collapsing spaces before using the rule in production text.