Ben Borgers

How to remove double spaces in a string in JavaScript

January 30, 2021

Here's how you can use regex in JavaScript to remove double spaces:

const string = 'Here is my sentence.  There are  double spaces.'

const newString = string.replace(/ {2,}/g, ' ')

This regex replaces any two or more spaces in a row with a single space.

More blog posts

Subscribe to my newsletter for a monthly round-up of new blog posts and projects Iā€™m working on!

Twitter ↗ RSS feed ↗