This?

str = str.replace(/\s/g, '');

Example

var str = '/var/www/site/Brand new document.docx';

document.write( str.replace(/\s/g, '') );
Run code snippetEdit code snippet Hide Results Copy to answer Expand


Update: Based on this question, this:

str = str.replace(/\s+/g, '');

is a better solution. It produces the same result, but it does it faster.

The Regex

\s is the regex for "whitespace", and g is the "global" flag, meaning match ALL \s (whitespaces).

A great explanation for + can be found here.

As a side note, you could replace the content between the single quotes to anything you want, so you can replace whitespace with any other string.

Answer from Šime Vidas on Stack Overflow
🌐
Reddit
reddit.com › r/learnjavascript › ways to remove spaces from a string using javascript
r/learnjavascript on Reddit: Ways to remove spaces from a string using JavaScript
January 11, 2023 - Progress: 100% replaceAll: 902 907 ops/s, ±1.30% | fastest replace: 811 102 ops/s, ±0.42% | 10.17% slower split-join: 519 097 ops/s, ±1.99% | slowest, 42.51% slower spread-filter-join: 624 614 ops/s, ±0.41% | 30.82% slower Finished 4 cases! Fastest: replaceAll Slowest: split-join Running "Remove spaces (1000 characters string)" suite...
Discussions

Remove whitespace Best Practice !?
hi everyone if you could rate my solution for that challenge, could you consider it Best Practice or Less or More (Average) or Newbie?? here my solution: let hello = " Hello, World! "; let wsRegex = /\s/g; // Chan… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
5
0
May 13, 2019
Removing leading and trailing spaces in JS strings
Hey everyone! I’m working on a project and I need some help with string manipulation in JavaScript. I want to clean up user input by getting rid of any extra spaces at the beginning and end of the text. What’s the best way to do this? I’ve tried using regular expressions, but I’m not ... More on community.latenode.com
🌐 community.latenode.com
0
0
March 13, 2025
Ways to remove spaces from a string using JavaScript
Thanks for sharing. I just did a performance test. The average time it takes each function (10,000 runs) to remove the spaces from a text with 1800 words/spaces in it. replaceAll: 0.08585000002384185 miliseconds replace: 0.10449000005722046 miliseconds splitAndJoin: 0.2322219943579563 miliseconds filterAndJoin: 1.1504000001549721 miliseconds Seems like replaceAll is the fastest and filterAndJoin is the slowest. More on reddit.com
🌐 r/learnjavascript
33
204
January 11, 2023
How do I remove the automatic white space between divs so they sit nicely on top of eachother?
It's not the divs, it's the paragraphs that have margin top and bottom. So, div p{ margin:0; } Edited: Ok, true that specificity is key, then more like this: .hello1 p, .hello2 p, .hello3 p, .hello4 p{ margin:0; } Also, I'd remove the spaces in your HTML around the equal signs where you are assigning the class name I.e., class="hello1" More on reddit.com
🌐 r/css
10
5
June 17, 2017
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › trim
String.prototype.trim() - JavaScript | MDN
July 10, 2025 - The trim() method of String values removes whitespace from both ends of this string and returns a new string, without modifying the original string.
🌐
W3Schools
w3schools.com › jsref › jsref_trim_string.asp
W3Schools.com
Sign In ★ +1 Get Certified Upgrade ... Academy Spaces Practice ... HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP W3.CSS C C++ C# HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS ... JS String JS ...
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Remove whitespace Best Practice !? - Curriculum Help - The freeCodeCamp Forum
May 13, 2019 - hi everyone if you could rate my solution for that challenge, could you consider it Best Practice or Less or More (Average) or Newbie?? here my solution: let hello = " Hello, World! "; let wsRegex = /\s/g; // Change this line let result = hello.replace(wsRegex, "").replace(/,/g, ", "); // Change this
🌐
DEV Community
dev.to › natclark › removing-whitespace-from-strings-in-javascript-4k32
Removing Whitespace From Strings in JavaScript - DEV Community
September 7, 2021 - However, "Example string" (with two spaces) will become "Example string" (with one space). If you just want to remove the trailing whitespace at the beginning and end of a string (if any), the trim() function is what you're looking for:
Find elsewhere
🌐
Medium
medium.com › @mohammedhammoud › how-to-remove-extra-spaces-in-string-javascript-cb66a314643a
How to remove extra spaces in a JavaScript String | by Mohammed Hammoud | Medium
July 20, 2017 - How to remove extra spaces in a JavaScript String Recently I wanted to remove extra spaces inside of string. The easiest way to do that was by extending JavaScript’s String object. You could …
🌐
Sololearn
sololearn.com › en › Discuss › 2734075 › how-to-remove-all-whitespaces-and-line-breaks-in-js-
How to remove all whitespaces and line breaks in js ?
March 23, 2021 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
CoreUI
coreui.io › answers › how-to-trim-whitespace-from-a-string-in-javascript
How to trim whitespace from a string in JavaScript · CoreUI
May 19, 2026 - Use the trim() method to remove leading and trailing whitespace from strings in JavaScript efficiently.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-remove-spaces-from-a-string-using-javascript
How to Remove Spaces From a String using JavaScript? - GeeksforGeeks
The _.trim() method method removes leading and trailing whitespace from a string. ... JavaScript string.replace() method is used to replace a substring. With Regular expressions pattern we can find all spaces (/\s/g) and globally replace all ...
Published   July 12, 2025
🌐
Medium
tariibaba.medium.com › javascript-remove-all-spaces-from-string-75f942ef2fbe
How to Remove All Spaces from a String in JavaScript | Medium
June 22, 2022 - To remove all spaces from a string in JavaScript, call the replaceAll() method on the string, passing a string containing a space as the first argument and an empty string ('') as the second.
🌐
Futurestud.io
futurestud.io › tutorials › remove-all-whitespace-from-a-string-in-javascript
Remove All Whitespace From a String in JavaScript
Luckily, JavaScript’s string.replace() method supports regular expressions. This tutorial shows you how to remove all whitespace from a string value.
🌐
Futurestud.io
futurestud.io › tutorials › remove-extra-spaces-from-a-string-in-javascript-or-node-js
Remove Extra Spaces From a String in JavaScript or Node.js
Use JavaScript’s string.replace() method with a regular expression to remove extra spaces. The dedicated RegEx to match any whitespace character is \s.
🌐
Educative
educative.io › answers › how-to-remove-whitespaces-from-the-beginning-of-a-string-in-js
How to remove whitespaces from the beginning of a string in JS
Note: We have removed the whitespaces from the beginning of the string using trimStart() method so the length of the trimmedStr is reduced.
🌐
Medium
medium.com › dailyjs › how-to-trim-string-in-javascript-43a898befce9
How to Trim String in JavaScript. It’s super simple to remove whitespace… | by Samantha Ming | DailyJS | Medium
April 27, 2020 - How to Trim String in JavaScript It’s super simple to remove whitespace from a string. To remove just the leading whitespace, you can use trimStart(). To remove trailing whitespace, use trimEnd() …
🌐
Code Beautify
codebeautify.org › remove-extra-spaces
Remove Extra Spaces Online: Whitespace and Tab
Extra Spaces Remover Online works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari. Text with extra spaces Try it. ... Buy us a Coffee JSON Formatter FAQ Privacy Policy Content Policy About Contact History Where am I right now?
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript | MDN
The replace() method of String values returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence ...
🌐
Beautifier
beautifier.io
Online JavaScript beautifier
Beautify JavaScript, JSON, React.js, HTML, CSS, SCSS, and SASS · Enable Light Mode · Beautify CSS · Beautify HTML · Beautify JavaScript · Beautify Code (ctrl‑enter) Copy to Clipboard Download Select All Clear · Indent with a tab character · Indent with 2 spaces · Indent with 3 spaces · Indent with 4 spaces · Indent with 8 spaces · Remove all extra newlines ·
🌐
Latenode
community.latenode.com › other questions › javascript
Removing leading and trailing spaces in JS strings
March 13, 2025 - Hey everyone! I’m working on a project and I need some help with string manipulation in JavaScript. I want to clean up user input by getting rid of any extra spaces at the beginning and end of the text. What’s the best w…
🌐
ReqBin
reqbin.com › code › javascript › zcdulg8f › javascript-trim-string-example
How to trim a string using JavaScript?
To remove spaces from a string using JavaScript, you can use the string.trim() method. The trim() method removes spaces from both ends of a given string without changing the original string.