For space-character removal use
"hello world".replace(/\s/g, "");
for all white space use the suggestion by Rocket in the comments below!
Answer from Henrik Andersson on Stack Overflow Top answer 1 of 7
418
For space-character removal use
"hello world".replace(/\s/g, "");
for all white space use the suggestion by Rocket in the comments below!
2 of 7
61
You can use
"Hello World ".replace(/\s+/g, '');
trim() only removes trailing spaces on the string (first and last on the chain).
In this case this regExp is faster because you can remove one or more spaces at the same time.
If you change the replacement empty string to '$', the difference becomes much clearer:
var string= ' Q W E R TY ';
console.log(string.replace(/\s/g, '

E$$
TY$
console.log(string.replace(/\s+/g, '#')); // #Q#W#E#R#TY#
Performance comparison - /\s+/g is faster. See here: http://jsperf.com/s-vs-s
Update
You can use too:
console.log(string.replaceAll(/\s/, '#')); // #Q#W#E#R#TY#
String.prototype.replaceAll()
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › trim
String.prototype.trim() - JavaScript | MDN
The trim() method of String values removes whitespace from both ends of this string and returns a new string, without modifying the original string.
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
How to remove whitespace from beginning and end of text in JavaScript
I’m working on a JavaScript project and I need to clean up user input by removing extra spaces. I have strings that sometimes have spaces, tabs, or other whitespace characters at the beginning or end. For example, when users type in a form field, they might accidentally add spaces before ... More on community.latenode.com
Removing Whitespace from a String
Hi all, Im working on massaging a string that has a lot of whitespace. My thought is to use split on space and then join all the non-empty values. My issue is when I use split, nothing changes. Not even on my test examples. Looking for Ideas on how to remove whitespace or a way to get split ... More on community.make.com
How to remove spaces from String?
I want to combine two expressions to create a bucket in aws. The bucket name cannot have any spaces. How would I remove spaces from the strings? ex. Bucket Name: {{ $(‘Merge’).item.json.Client }}-{{ $(‘Merge’).item.json[‘Title of Project’] }} | OUTPUT would be: New York-Marketing ... More on community.n8n.io
02:54
How to remove Whitespace from Strings in Javascript - YouTube
00:35
Remove Whitespace in JavaScript - YouTube
02:57
JavaScript Regular Expressions: Remove Whitespace from Start and ...
02:33
Frequently Asked Java Program 25: How To Remove White Spaces in ...
00:40
How to Remove White Spaces in String with JavaScript (RegEx) - YouTube
Mastering JavaScript: Effortlessly Remove Whitespace from ...
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...
W3Schools
w3schools.com › jsref › jsref_trim_string.asp
JavaScript String trim() Method
"; let result = text.trim(); Try it Yourself » · Remove spaces with replace() using a regular expression: let text = " Hello World! "; let result = text.replace(/^\s+|\s+$/gm,''); Try it Yourself » · The trim() method removes whitespace ...
DEV Community
dev.to › natclark › removing-whitespace-from-strings-in-javascript-4k32
Removing Whitespace From Strings in JavaScript - DEV Community
September 7, 2021 - If you just want to remove the space character and not all whitespace, this snippet will do the trick: const string = `This is an example string.`; string.replace(/ /g, ``); Keep in mind, it won't remove consecutive spaces or tabs. For example, "Example string" will become "Examplestring". However, "Example string" (with two spaces) will become "Example string" (with one space).
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-remove-spaces-from-a-string-using-javascript
How to Remove Spaces From a String using JavaScript? - GeeksforGeeks
JavaScript string.replaceAll() method replaces all occurrences of a substring from a string. It will replace all the whitespaces with an empty string. ... The JavaScript string.trim() method removes leading and trailing whitespace from a string.
Published July 12, 2025
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
Obfuscator
obfuscator.io
JavaScript obfuscator tool - Protect Your JS Code
There are numerous reasons to protect your code: prevent anyone from simply copy/pasting your work (especially important for client-side projects like HTML5 games), remove comments and whitespace to make code faster to load and harder to understand, and protect work that hasn't been paid for yet so you can show clients without giving them the source code. What is VM obfuscation and how is it different from standard obfuscation? VM (Virtual Machine) obfuscation transforms your JavaScript code into custom bytecode that runs on an embedded interpreter.
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 - The regular expression we specified only matches spaces in the string. To match and remove all whitespace characters (spaces, tabs and newlines), we’ll have to use a different regex: const str = 'A B C \t D \n E'; const whitespaceRemoved = str.replace(/\s/g, '');console.log(whitespaceRemoved); // ABC ... A captivating guide to the subtle caveats and lesser-known parts of JavaScript.
n8n
community.n8n.io › questions
How to remove spaces from String? - Questions - n8n Community
June 27, 2025 - I want to combine two expressions to create a bucket in aws. The bucket name cannot have any spaces. How would I remove spaces from the strings? ex. Bucket Name: {{ $(‘Merge’).item.json.Client }}-{{ $(‘Merge’).item.json[‘Title of Project’] }} | OUTPUT would be: New York-Marketing Campaign OUTPUT should be: NewYork-MarketingCampaign