DEV Community

Cover image for How to Remove Extra Spaces From Text: 5 Practical Ways !
narayan
narayan

Posted on

How to Remove Extra Spaces From Text: 5 Practical Ways !

If you've ever copied text from a PDF, website, email, or document, you've probably ended up with something like this:

This   is    some    messy    text.
Enter fullscreen mode Exit fullscreen mode

It's a tiny problem, but it becomes surprisingly annoying when you're processing a large amount of text.

In this post, I'll go through a few practical ways to remove extra spaces, from simple Find and Replace to JavaScript, Python, spreadsheets, and an online tool.

1. Use Find and Replace

For a small amount of text, the simplest solution is often the best one.

Most text editors have Find and Replace.

You can search for multiple consecutive spaces and replace them with a single space.

For example:

This    is     some    text.
Enter fullscreen mode Exit fullscreen mode

becomes:

This is some text.
Enter fullscreen mode Exit fullscreen mode

The downside is that you may have to repeat the operation if the text contains different numbers of consecutive spaces.


2. Remove Extra Spaces With JavaScript

If you're already working with JavaScript, this is probably the easiest solution to automate.

const text = "This   is    some    messy text.";

const cleaned = text
  .replace(/\s+/g, " ")
  .trim();

console.log(cleaned);
Enter fullscreen mode Exit fullscreen mode

Output:

This is some messy text.
Enter fullscreen mode Exit fullscreen mode

The important part is:

.replace(/\s+/g, " ")
Enter fullscreen mode Exit fullscreen mode

The \s+ pattern matches one or more whitespace characters and replaces them with a single space.

The .trim() removes whitespace from the beginning and end of the string.

Be careful with line breaks

There is one important detail here.

\s doesn't only match spaces. It also matches newlines and other whitespace characters.

So this:

Hello

World
Enter fullscreen mode Exit fullscreen mode

could become:

Hello World
Enter fullscreen mode Exit fullscreen mode

If you want to preserve line breaks, process each line separately:

const cleaned = text
  .split("\n")
  .map(line => line.replace(/[ \t]+/g, " ").trim())
  .join("\n");
Enter fullscreen mode Exit fullscreen mode

Here we're only replacing spaces and tabs within each line.


3. Remove Extra Spaces With Python

The same idea is simple in Python.

import re

text = "This   is    some    messy text."

cleaned = re.sub(r"\s+", " ", text).strip()

print(cleaned)
Enter fullscreen mode Exit fullscreen mode

Output:

This is some messy text.
Enter fullscreen mode Exit fullscreen mode

Again, keep in mind that \s+ also matches newlines.

If preserving the structure of the text matters, you should handle lines separately rather than replacing every whitespace character.


4. Use a Spreadsheet

If you're working with text stored in Excel or Google Sheets, you don't necessarily need to write code.

The TRIM function can remove unnecessary spaces.

For example:

=TRIM(A1)
Enter fullscreen mode Exit fullscreen mode

If A1 contains:

This   is    some    messy text
Enter fullscreen mode Exit fullscreen mode

the result becomes:

This is some messy text
Enter fullscreen mode Exit fullscreen mode

This is particularly useful when you're cleaning a column containing lots of text.


5. Use an Online Text Cleaner

Sometimes you don't need to write a script at all.

If I just need to clean some copied text quickly, an online tool is more convenient than opening an editor or writing a small script.

I built FastTextCleaner for this kind of situation. You can paste messy text, enable the extra-space option, clean it, and copy the result.

You can try it here:

https://fastextcleaner.com/remove-extra-spaces/

The tool processes the text directly in the browser, so there's no setup required.


Spaces vs. Line Breaks

This distinction is worth paying attention to when writing a text-cleaning script.

These are different problems:

Hello     world
Enter fullscreen mode Exit fullscreen mode

and:

Hello
world
Enter fullscreen mode Exit fullscreen mode

The first contains extra spaces.

The second contains a line break.

If you're cleaning text copied from a PDF, you might have both problems at the same time.

For example:

This   is a sentence
that was copied
from a PDF.
Enter fullscreen mode Exit fullscreen mode

You might want:

This is a sentence that was copied from a PDF.
Enter fullscreen mode Exit fullscreen mode

But blindly replacing every whitespace character with a space can destroy formatting that you actually wanted to keep.

That's why a text-cleaning script should be designed around the type of text you're processing.

A Simple JavaScript Rule

If you're working with normal paragraphs and don't care about preserving line breaks, this is usually enough:

const cleaned = text
  .replace(/\s+/g, " ")
  .trim();
Enter fullscreen mode Exit fullscreen mode

If you want to preserve line breaks:

const cleaned = text
  .split("\n")
  .map(line => line.replace(/[ \t]+/g, " ").trim())
  .join("\n");
Enter fullscreen mode Exit fullscreen mode

The second approach gives you more control over the structure of the original text.

Which Method Should You Use?

It depends on what you're trying to clean.

Situation Good option
A few lines of text Find and Replace
JavaScript application Regular expression
Python script re.sub()
Spreadsheet data TRIM()
Quick one-off cleaning Online text cleaner
Text where line breaks matter Process each line separately

There's no need to overcomplicate a simple problem.

Final Thoughts

Extra spaces are one of those small formatting problems that are easy to ignore until you're dealing with a large amount of messy text.

For a few lines, Find and Replace is usually enough.

For code, a small regular expression can solve it.

For datasets, spreadsheet functions or a script can be more practical.

And when you just want to paste some text, clean it, and move on, an online text cleaner can save some time.

The main thing to remember is that spaces, tabs, and line breaks aren't always the same thing. Before cleaning text programmatically, decide which whitespace you actually want to remove and which formatting you need to preserve.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.