How to Replace White Space in Strings using Regex in JavaScript
Table of Contents
Regular expressions are a powerful way to work with and manipulate strings.
One of the most common operations you will perform on strings is to replace whitespace with another character, usually nothing (removing whitespace).
In this post, we'll learn how we can use regex to replace whitespace in a string in JavaScript.
How to Replace Whitespace in a String in JavaScript
To replace whitespace in a string in JavaScript, you can use the replace()
method.
This method returns a new string with some or all matches of a pattern replaced by a replacement.
First let's define a long sentence:
JAVASCRIPTconst sentence = "The quick brown fox jumps over the lazy dog.";
console.log(sentence);
BASHThe quick brown fox jumps over the lazy dog.
Now let's remove all the white space in this sentence by replacing all the white space with nothing:
JAVASCRIPTconst sentence = "The quick brown fox jumps over the lazy dog.";
sentence.replace(/\s/g, "");
console.log(sentence);
BASHThequickbrownfoxjumpsoverthelazydog.
The reason why this work is that we are using a regular expression to match all the white space in the string.
The regular expression we used is /\\s/g
which matches all the white space in the string.
The global flag g
is used to replace all the white space in the string, instead of just the first match.
Finally, we are replacing the white space with a blank string, which is why the string is now all one word.
If we wanted to replace the white space with a dash, we could do this:
JAVASCRIPTconst sentence = "The quick brown fox jumps over the lazy dog.";
sentence.replace(/\s/g, "-");
console.log(sentence);
BASHThe-quick-brown-fox-jumps-over-the-lazy-dog.
How to Count how many Whitespace Characters are in a String in JavaScript
To count how many whitespace characters are in a string in JavaScript, you can use the match()
method.
We can use the same regular expression we used to replace the white space to count how many white space characters are in the string.
First let's define a long sentence:
JAVASCRIPTconst sentence = "The quick brown fox jumps over the lazy dog.";
console.log(sentence);
BASHThe quick brown fox jumps over the lazy dog.
Now let's count how many white space characters are in this sentence:
JAVASCRIPTconst sentence = "The quick brown fox jumps over the lazy dog.";
const length = sentence.match(/\s/g).length;
console.log(length);
BASH8
Conclusion
In this post, we learned how to replace whitespace in a string in JavaScript.
Simply use the replace()
method and pass in a regular expression to match all the white space in the string, and then replace it with another character.
We also learned how to count how many whitespace characters are in a string in JavaScript by using the match()
method.
Thanks for reading!
- Getting Started with TypeScript
- Getting Started with Express
- Create an RSS Reader in Node
- Getting Started with Electron
- Git Tutorial: Learn how to use Version Control
- How to deploy a PHP app using Docker
- Using Puppeteer and Jest for End-to-End Testing
- How to Scrape the Web using Node.js and Puppeteer
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Using Push.js to Display Web Browser Notifications
- Getting Started with React
- How To Create a Modal Popup Box with CSS and JavaScript