How to Remove Extra Spaces from Text (Online Tool + Code)

3 min read

Extra spaces creep into text from copy-paste, OCR, and word processors. Here's how to clean them.

Regex

// Collapse multiple spaces to one
text.replace(/  +/g, ' ')

// Trim each line
text.split('\n').map(l => l.trim()).join('\n')

// Remove extra blank lines
text.replace(/\n{3,}/g, '\n\n')

Online Tool

Use our plain text converter with "Collapse extra spaces" enabled.