Extra spaces sneak in from everywhere. Copy text from a PDF and the line breaks turn into spaces. Paste from a Word document and you get double spaces after every period - a habit left over from typewriter days. Pull data from a spreadsheet and each cell has invisible leading or trailing spaces that break your sort order. Even well-intentioned typing produces them when you accidentally hit the spacebar twice.
The problem is that most of these extras are invisible. Your text looks fine until it doesn't - until a string comparison fails, a database query returns no results, or your published article has awkward gaps that readers notice immediately. This guide covers every practical way to find and remove them.
Types of extra spaces you might be dealing with
| Type | Where it comes from | How to spot it |
|---|---|---|
| Double spaces | Typing habit, typewriter-style formatting | Two spaces between words or after periods |
| Leading spaces | Copy-paste, CSV exports, form submissions | Space at the start of a line or value |
| Trailing spaces | Copy-paste, code editors, text exports | Invisible space at end of line or value |
| Non-breaking spaces | Copy from web pages, HTML content | Looks normal but behaves differently; in HTML |
| Multiple blank lines | Copy from PDFs, Word documents | Extra vertical space between paragraphs |
Browser Tool: The Fastest Option
If you just need to clean up a block of text right now without writing any code, the Article Formatter handles it in one click. Paste your text into the input area and click Format. It automatically:
- Collapses multiple consecutive spaces into a single space
- Strips leading and trailing spaces from the entire text
- Converts non-breaking spaces (the invisible HTML entities) into regular spaces
- Removes extra blank lines between paragraphs
Nothing is sent to a server - everything runs in your browser. This is particularly useful when you've just copied text from a PDF or Word document and it came out mangled. The formatter also fixes other common paste problems at the same time, like smart quotes and encoding artifacts, so you get a clean result without multiple steps. If you're dealing with text that came from a PDF specifically, the guide to cleaning up PDF text covers additional issues that come with that workflow.
Microsoft Word: Find and Replace
Word's Find and Replace (Ctrl+H on Windows, Cmd+H on Mac) is the go-to for cleaning up a document you're actively editing. The basic approach:
- Open Find and Replace (Ctrl+H)
- In the Find field, type two spaces
- In the Replace field, type one space
- Click Replace All
- Repeat until the count shows zero replacements
The "repeat until zero" step is important. If you have three spaces in a row, one pass turns it into two, not one. A second pass finishes the job. Most documents need two or three passes at most.
For leading and trailing spaces on each line, Word doesn't have a direct single-step option. You can use a macro or switch to a regex-capable tool (VS Code, Notepad++, or a browser tool). For removing trailing spaces from the end of paragraphs specifically, enable "Use wildcards" in Find and Replace and search for @^13 (a space followed by a paragraph mark) and replace with ^13.
Word also has a built-in paragraph spacing cleaner. Go to Home > Paragraph > Line Spacing Options. If your extra spacing is actually extra line spacing rather than extra space characters, adjust the "Before" and "After" spacing values there.
Google Docs: Find and Replace
Google Docs uses the same Find and Replace approach as Word. Open it with Ctrl+H (Cmd+H on Mac). Type two spaces in the Find field, one space in the Replace field, and click Replace All. Same rule applies - repeat until the count hits zero.
Google Docs also supports regular expressions in Find and Replace. Check the "Match using regular expressions" box and use + (a space followed by a plus) in the Find field with a single space in the Replace field. This catches two, three, or any number of consecutive spaces in a single pass. For leading and trailing spaces on each paragraph, regex support in Docs is limited - a browser tool or code is more reliable.
Notepad++ and VS Code: Regex Find and Replace
Both editors support regex search, which makes this much faster than repeated Find and Replace passes.
In Notepad++:
- Open Find and Replace (Ctrl+H)
- Check "Regular expression" at the bottom
- Find:
[ ]{2,}(two or more spaces) - Replace: one space
- Click Replace All
In VS Code:
- Open Find and Replace (Ctrl+H)
- Click the .* button to enable regex mode
- Find:
{2,}(two or more spaces) - Replace: one space
- Click Replace All
For trailing spaces specifically, VS Code has a built-in setting that removes trailing whitespace on save: add "files.trimTrailingWhitespace": true to your settings.json. Notepad++ has a similar option under Edit > Blank Operations > Trim Trailing Space.
JavaScript: trim() and replace()
JavaScript has two built-in methods that handle the common cases cleanly:
const raw = " Hello world. Too many spaces. ";
// Remove leading and trailing spaces
const trimmed = raw.trim();
// "Hello world. Too many spaces."
// Collapse multiple spaces into one
const cleaned = raw.trim().replace(/ +/g, ' ');
// "Hello world. Too many spaces."
The / +/g regex matches one or more consecutive spaces and replaces them with a single space. The g flag applies it to every match in the string, not just the first one.
If you also want to collapse tabs into single spaces (common with copy-paste from spreadsheets), use \s+ instead of +:
// Handle spaces, tabs, and other whitespace
const cleaned = raw.trim().replace(/\s+/g, ' ');
One edge case: \s also matches newlines, so this approach flattens multi-line text into a single line. If you want to preserve line breaks but only clean spaces within each line:
// Clean spaces within lines, preserve line breaks
const cleaned = raw
.split('\n')
.map(line => line.trim().replace(/ +/g, ' '))
.join('\n'); Python: strip(), split(), and re.sub()
Python gives you several options. For simple cases, the built-in string methods handle it without importing anything:
# Remove leading and trailing whitespace
text = " Hello world. "
stripped = text.strip()
# "Hello world."
# Collapse multiple spaces - split on whitespace, rejoin with single space
cleaned = ' '.join(text.split())
# "Hello world."
The split() trick is clever: calling it with no arguments splits on any whitespace (spaces, tabs, multiple spaces) and automatically ignores leading and trailing whitespace. Joining with a single space gives you a clean result in one line. The downside is it also collapses newlines, so it works best for single-line values.
For multi-line text where you want to preserve paragraph breaks:
import re
text = """ Hello world.
Too many spaces here. """
# Clean each line individually, then rejoin
cleaned = '\n'.join(' '.join(line.split()) for line in text.splitlines())
print(cleaned)
# "Hello world.
#
# Too many spaces here."
If you prefer regex directly, re.sub() handles it in one step:
import re
text = " Hello world. Too many spaces. "
# Trim + collapse, single line
cleaned = re.sub(r' +', ' ', text).strip()
# "Hello world. Too many spaces."
# Or use \s+ to also handle tabs
cleaned = re.sub(r'[^\S\n]+', ' ', text).strip()
# [^\S\n] matches whitespace that isn't a newline
When processing CSV files where cells may have leading or trailing spaces, apply strip() per cell:
import csv
with open('input.csv', newline='') as f:
reader = csv.reader(f)
rows = [[cell.strip() for cell in row] for row in reader]
# Write cleaned data back
with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(rows) Command Line: sed and awk
For batch processing files on Linux, macOS, or Windows Subsystem for Linux:
# Collapse multiple spaces into one (sed)
sed 's/ */ /g' input.txt > output.txt
# Same with extended regex (-E), cleaner syntax
sed -E 's/ +/ /g' input.txt > output.txt
# Remove leading spaces from each line
sed 's/^ *//' input.txt > output.txt
# Remove trailing spaces from each line
sed 's/ *$//' input.txt > output.txt
# Do all three in one pass
sed -E 's/^ +| +$//g; s/ +/ /g' input.txt > output.txt
On macOS, sed is BSD sed, which needs -E for extended regex instead of -r. The behavior is otherwise the same.
awk can also handle this, especially useful when you're already using it for other processing:
# awk: trim and collapse spaces on each line
awk '{$1=$1; print}' input.txt
The $1=$1 trick forces awk to reparse the record, which collapses internal whitespace and strips leading/trailing spaces. It's a quick one-liner for simple cases.
Non-Breaking Spaces: The Hidden Problem
Regular spaces and non-breaking spaces look identical. But they are different characters. A regular space is Unicode U+0020. A non-breaking space is U+00A0 - it appears in HTML as and is used to prevent line breaks between words.
The problem: when you copy text from a web page or PDF, those non-breaking spaces come along. They look like spaces, but Find and Replace for a regular space won't catch them. String comparisons will fail. Sorting won't work right. A cell in Excel that appears blank might actually contain a non-breaking space.
How to handle them:
// JavaScript: replace non-breaking spaces with regular spaces
const cleaned = text.replace(/\u00A0/g, ' '); // \u00A0 = non-breaking space
// Or replace ALL non-standard whitespace characters
const cleaned = text.replace(/\u00A0/g, ' '); // extend to match other spaces # Python: replace non-breaking spaces
cleaned = text.replace('\u00a0', ' ')
# Or normalize all Unicode whitespace at once
import unicodedata
cleaned = ''.join(' ' if unicodedata.category(c) == 'Zs' else c for c in text)
In Word, you can Find and Replace non-breaking spaces by typing ^s in the Find field (the caret-s code represents a non-breaking space in Word). Replace with a regular space.
The Article Formatter automatically converts non-breaking spaces to regular spaces as part of its cleanup, so if you're working with copy-pasted web content this is the fastest fix.
Tips for Better Results
- Clean before you process. If you're loading text data into a database or running comparisons, strip spaces at the input stage. Fixing data after it's stored is much harder than preventing the problem.
- Check for non-breaking spaces first. If your Find and Replace isn't catching spaces you can see, they're probably non-breaking spaces. Paste into a text editor that shows invisible characters to confirm.
- Regex is worth learning for this. A regex replacement handles double, triple, and quadruple spaces in one pass. Without regex, you're doing repeated Find and Replace and hoping you got them all.
- Test on a copy. Before running a regex replacement on a large document, test on a small sample to confirm your pattern doesn't accidentally remove spaces you want to keep.
- Spreadsheet cells often have trailing spaces. If your VLOOKUP or MATCH formulas aren't finding values you can clearly see, trailing spaces in the lookup column are the most common cause. Use Excel's TRIM() function or Python's strip() to fix them.
- Version control for code. Configure your editor to strip trailing whitespace on save. It's a small setting that prevents a lot of noisy diffs and merge conflicts.
Frequently Asked Questions
What is the fastest way to remove extra spaces from text?
How do I remove extra spaces in Microsoft Word?
^s in the Find field and replace with a regular space.
How do I remove extra spaces using JavaScript?
str.trim().replace(/ +/g, ' ') to remove leading and trailing spaces and collapse multiple spaces into one. If you also want to handle tabs, use str.trim().replace(/\s+/g, ' ') - but note that this also flattens newlines.
Why won't Find and Replace remove all my extra spaces?
^s to find non-breaking spaces.
What is a non-breaking space and how do I remove it?
) looks identical to a regular space but is a different character. It's used in HTML to prevent line breaks. When you copy from web pages, these come along. In JavaScript, replace with str.replace(/\u00A0/g, ' '). In Python, use text.replace('\u00a0', ' '). The Article Formatter handles them automatically.