Regex Pattern

Remove All Whitespace Regex

By Samrat•Last updated: Nov 2026
Regex Pattern
\s+

Quick answer

This regex matches one or more whitespace characters. Replacing it with an empty string removes all spaces from the text.

🛠️ Fix it right here with AI Regex Generator

Need to modify this pattern or generate a new one? Edit the regex below and let our AI help.

AI suggestions can be wrong. Review and test before applying.

Live Regex Tester

Type a string below to see if it matches this pattern.

How it works

\s matches any whitespace character (space, tab, newline). The + makes it match consecutive whitespaces together, which is more efficient than replacing them one by one.

Code Examples

✅ Correct Usage

// JavaScript
const str = "Hello   World!\n";
const clean = str.replace(/\s+/g, "");
console.log(clean); // "HelloWorld!"

❌ What NOT to do

// BAD: Only removes standard spaces, leaves tabs and newlines
const badRegex = / /g;

Frequently asked questions

How do I remove only spaces, but keep newlines?

Use /[ \t]+/g instead of \s+, or replace spaces specifically: / +/g.

Related Search Terms

remove whitespace regexregex remove spacesstrip spaces javascriptregex replace space

Related Regex Patterns

Need a custom regex pattern?

Use the free to describe what you need in plain English.

Want an AI with memory for your project?

MonsterX AI remembers your codebase across chats.