JavaScript Program to Remove All Whitespaces From a Text
Last Updated :
04 Oct, 2023
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.
We will explore all the above approaches with the help of illustration.
Using Regular Expressions (regex)
Regular expressions are powerful tools for pattern matching. They can be used to find and replace white spaces with an empty string or any desired character in a text.
Syntax
const stringWithoutSpaces = stringWithSpaces.replace(/\s/g, '');
Example: This example illustrates the use of the Regular Expressions to filter out all Whitespaces from a Text.
JavaScript
const removeSpaces = (inputText) => {
return inputText.replace(/\s/g, "");
};
console.log(
removeSpaces(
`GeeksforGeeks is the best
platform for learning.`
)
);
OutputGeeksforGeeksisthebestplatformforlearning.
Using String Split and Join
This method can split the text into an array of words based on white space and then join the array elements back together without spaces, effectively removing the white spaces.
Syntax
const stringWithoutSpaces = stringWithSpaces.split(' ').join('');
Example: This example illustrates the use of the String Split and Join.
JavaScript
const removeSpaces = (inputText) => {
const words =
inputText.split(/\s+/);
return words.join("");
};
console.log(
removeSpaces(
`Geeks For Geeks`
)
);
Using the ES6 replace() method with a Regular Expression
The replace() method in JavaScript can be used with a regular expression to search for white spaces and replace them with another character or an empty string.
Syntax
const stringWithoutSpaces = stringWithSpaces.replace(/\s+/g, '');
Example: This example illustrates the use of the replace() method with a Regular Expression.
JavaScript
const removeSpaces = (inputText) => {
return inputText.replace(
/\s+/g,
""
);
};
console.log(
removeSpaces("Geeks For Geeks")
);
Using filter() and join() (functional approach)
This approach involves splitting the text into an array of words and then using the filter() function to remove empty elements (whitespace) from the array. Finally, the join() method is used to combine the remaining elements into a single string.
Syntax
const stringWithoutSpaces = stringWithSpaces.split('').filter(char => char !== ' ').join('');
Example: This example illustrates the use of the filter() and join() Methods.
JavaScript
const removeSpaces = (inputText) => {
const characters =
inputText.split("");
const nonEmptyCharacters =
characters.filter(
(char) => !/\s/.test(char)
);
return nonEmptyCharacters.join("");
};
console.log(
removeSpaces("Geeks for Geeks")
);
Similar Reads
JavaScript - How to Remove All Line Breaks From a String? Here are different ways to remove all line breakes from a string in JavaScript.1. Using JavaScript RegEx with replace() Method This method uses regular expressions to detect and replace newlines in the string. It is fed into replace function along with a string to replace with, which in our case is
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 Remove Whitespace in Ruby? In this article, we will learn how to remove whitespace in Ruby. Removing whitespace in Ruby eliminates spaces, tabs, carriage returns, and newlines from a string. Table of Content Using gsub Method with a Regular ExpressionUsing Delete Method with Specific CharactersUsing the Split and Join Methods
3 min read
How to remove white spaces from a string using jQuery ? In this article, we will see how to remove the white spaces from string using jQuery. To remove the white spaces, we will use trim() method. The trim() method is used to remove the white spaces from the beginning and end of a string. Syntax: jQuery.trim( str ) Parameter: This method accepts a single
1 min read
Batch Script - Remove All Spaces In this article, we are going to see how to remove all spaces from any string using Batch String. Example : Input: G e e k s f o r G e e k s Output: GeeksforGeeksApproach :By using ' set ' we are getting input of any string.Example: set str=input stringIn the next line using ' echo %str% ' we are pr
1 min read
JavaScript - How to Replace Multiple Spaces with a Single Space? 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.JavaScriptconst s1
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
Pandas - Strip whitespace from Entire DataFrame Pythonâs Pandas library has established itself as an essential tool for data scientists and analysts. Common task that users frequently encounter is the need to clean data, which often involves stripping whitespace from strings. In this article, we will explore how to effectively remove whitespace f
9 min read
How to remove blank lines from a .txt file in Python Many times we face the problem where we need to remove blank lines or empty spaces in lines between our text to make it look more structured, concise, and organized. This article is going to cover two different methods to remove those blank lines from a .txt file using Python code. This is going to
3 min read