December 2023
Intermediate to advanced
242 pages
5h 28m
English
| Recipe 61 | Removing Duplicate Spaces |
Suppose you have a website with a blog section that allows guest posts. As a reviewer, you have noticed a common mistake in the submitted posts: double spaces between words. To address this issue, you want to create a function that automatically identifies and corrects this error by replacing double spaces with a single space.
A quick and dirty way to convert double spaces to a single space is to use the replaceAll() method like this:
| | const str = "A day without sunshine is like, you know, night."; |
| | |
| | str.replaceAll(" ", " "); |
| | // → "A day without sunshine is like, you know, night." |
The first argument you pass to replaceAll() is the pattern ...
Read now
Unlock full access