JavaScript - How to Replace Multiple Spaces with a Single Space? Last Updated : 26 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Here are the various methods to replace multiple spaces with a single space in JavaScript.1. Using replace() Method with Regular Expression (Most Common)The replace() method combined with a regular expression is the most efficient way to replace multiple spaces with a single space. JavaScript const s1 = "This is a test string."; const s2 = s1.replace(/\s+/g, " "); console.log(s2); OutputThis is a test string. \s+: Matches one or more consecutive whitespace characters (spaces, tabs, newlines).g flag: Ensures the replacement is applied globally." ": Replaces matches with a single space.2. Using trim(), split(), and join() MethodsThis approach involves splitting the string into an array of words and then joining them back together with a single space. JavaScript const s1 = " This is a test string. "; const s2 = s1.trim().split(/\s+/).join(" "); console.log(s2); OutputThis is a test string. trim(): Removes leading and trailing spaces.split(/\s+/): Splits the string into an array of words using one or more spaces as the delimiter.join(" "): Joins the array back into a single string with a single space between each word.3. Using Regular Expression with replace() and trim() MethodsFor strings that may have leading or trailing spaces, combining trim() with a regular expression will be a better option JavaScript const s1 = " This is a test string. "; const s2 = s1.trim().replace(/\s+/g, " "); console.log(s2); OutputThis is a test string. trim(): Removes extra spaces at the beginning and end of the string.replace(/\s+/g, " "): Replaces multiple spaces within the string.4. Using a Loop and Conditional LogicIf you want full control over the replacement process, you can use a loop to process the string manually. JavaScript const s1 = "This is a test string."; let s2 = ""; let wasSpace = false; for (let char of s1) { if (char === " ") { if (!wasSpace) { s2 += char; wasSpace = true; } } else { s2 += char; // Reset the flag when a non-space character is found wasSpace = false; } } console.log(s2); OutputThis is a test string. The loop iterates through each character.A flag (wasSpace) ensures that consecutive spaces are replaced by a single space.The replace() method with a regular expression is the most efficient and commonly used approach to replace multiple spaces with a single space. For advanced cleanup, you can combine trim(), split(), and join(). Comment More infoAdvertise with us Next Article JavaScript - How to Replace Multiple Spaces with a Single Space? V vkash8574 Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Questions +1 More Similar Reads Replacing spaces with underscores in JavaScript Given a sentence and the task is to replace the spaces(" ") from the sentence with underscores("_") in JavaScript. There are some JavaScript methods are used to replace spaces with underscores which are listed below: JavaScript replace() method: This method searches a string for a defined value, or 2 min read Create a string with multiple spaces in JavaScript We have a string with extra spaces and if we want to display it in the browser then extra spaces will not be displayed. Adding the number of spaces to the string can be done in the following ways. In this article, we are going to learn how to Create a string with multiple spaces in JavaScript. Below 3 min read How to replace a portion of strings with another value in JavaScript ? In JavaScript, replacing a portion of strings with another value involves using the `replace()` method or regular expressions. This process is essential for text manipulation tasks like formatting, sanitization, or dynamic content generation in web development and data processing applications. We ca 3 min read How to Remove Spaces From a String using JavaScript? These are the following ways to remove space from the given string:1. Using string.split() and array.join() MethodsJavaScript string.split() method is used to split a string into multiple sub-strings and return them in the form of an array. The join() method is used to join an array of strings using 2 min read How to replace all dots in a string using JavaScript ? We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript. Table of Content Using JavaScript replace() MethodUsing JavaScript Split() and Join() Method Using JavaSccript reduce() Method and spread operatorUsing JavaScript replaceAll() 4 min read JavaScript string replace() Method JavaScript replace() method is used for manipulating strings. It allows you to search for a specific part of a string, called a substring, and then replace it with another substring. What's great is that this method doesn't alter the original string, making it ideal for tasks where you want to maint 5 min read JavaScript - Remove Text From a String Here are the different approaches to remove specific text from a string in JavaScript, starting from the most commonly used approachesUsing replace() Method - Best MethodThe replace() method is a direct and widely used method to remove specific text by replacing it with an empty string "". This meth 2 min read JavaScript replaceAt() Method The replaceAt() method is useful for removing the character at a given place. Unfortunately, the replaceAt() method is not a built-in method in JavaScript. It would need to be defined by the user in order to use it. One possible implementation of a replaceAt() function could take a string and two in 2 min read JavaScript Program to Remove All Whitespaces From a Text In this article, we will be eliminating spaces from a string and will explore the various types of cases that fall under the category of spaces Space character (â£):Tab character (TAB):Newline character (Line Feed or Enter) etc.Table of ContentUsing Regular Expressions (regex)Using String Split and J 2 min read How to Remove Spaces from a String in TypeScript ? TypeScript offers various inbuilt functions to remove spaces from a string. These functions can be used to remove spaces between characters or entire words. Below, we explore different approaches to remove spaces from a string in TypeScript.Table of ContentUsing split() and join() methodsUsing repla 4 min read Like