Ben Borgers

How to remove double spaces in a string in JavaScript

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.