This article will help you replace multiple spaces with a single space in JavaScript by using replace()
function, split()
, and join()
methods combined with regular expression. Let’s take a look at the methods below in detail.
Replace multiple spaces with a single space in JavaScript
Using replace() method and RegEx
In JavaScript, you must be familiar with the replace() function. It is used to replace the string of characters in the original string with another string you want.
And to replace multiple spaces into a single space in a string, we also use the replace()
function, with the first parameter being a regular expression representing the spaces in the string and the second argument just a space. It will replace many of the original spaces in the string.
Example:
let str = ' Learn Share IT '; console.log('Original string: '+ str); let newStr1 = str.replace(/ +/g, ' '); console.log('String after replacing multiple spaces: '+ newStr1); let newStr2 = str.replace(/\s+/g, ' '); console.log('String after replacing multiple spaces: '+ newStr2); let newStr3 = str.replace(/ {2,}/g, ' '); console.log('String after replacing multiple spaces: '+ newStr3);
Output
Original string: Learn Share IT
String after replacing multiple spaces: Learn Share IT
String after replacing multiple spaces: Learn Share IT
String after replacing multiple spaces: Learn Share IT
In the above example, you can see that we used 3 different regular expressions, but they all show multiple spaces in the search string. These are the regular expressions I’ve used and what they mean. There are many more regular expressions in Javascript that you can learn about in detail here.
+
: Check character appears one or more times.\s
: Find characters that are spaces.{2,}
: Check the character appears at least 2 times.g
: Compares the entire search string.
Using split() and join() method
The second way to replace multiple spaces into one space is using the split()
method to split the original string into an array of ['Learn', 'Share', 'IT']
. Then use the join()
function to join the elements together. The parameter in the join()
function will be a space, which is the distance between the elements.
Example:
let str = ' Learn Share IT '; console.log('Original string: '+ str); let newStr4 = str.split(/ +/g).join(' '); console.log('String after replacing multiple spaces: '+ newStr4); let newStr5 = str.split(/\s+/g).join(' '); console.log('String after replacing multiple spaces: '+ newStr5); let newStr6 = str.split(/ {2,}/g).join(' '); console.log('String after replacing multiple spaces: '+ newStr6);
Output
Original string: Learn Share IT
String after replacing multiple spaces: Learn Share IT
String after replacing multiple spaces: Learn Share IT
String after replacing multiple spaces: Learn Share IT
Summary
So, you already know how to replace multiple spaces with a single space in JavaScript by using replace()
function and split()
and join()
methods combined with regular expression. Many regular expressions help us find a lot of gaps, but you need to remember a simple method that is easy to apply or know how to build those expressions. Good luck with your practice. See you in the next post.
Maybe you are interested:
- Add specific Number of Spaces to a String in JavaScript
- Check if String contains only Spaces in JavaScript
- Remove leading and trailing Spaces from a String using JavaScript

Hi, my name is Joni Smith. My job is a programmer. I love learning programming languages, especially C++, C#, php, javascript, html, css. I want to share them with you. I hope my articles will bring a lot of useful knowledge to you.
Name of the university: HVNH BA
Major: htttql MIS
Programming Languages: C++, C#, php, javascript, html, css