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.

Subscribe to my newsletter!

A weekly round-up of new blog posts and updates to projects I’m working on.