I have come across the following regular expression being applied to strings:
.replace(/\s+/g, " ")
What does this do?
It finds each instance of a double space (or more) in a string and replaces it with a single space.
Here's an example:
let string = " A string with many spaces. "; string = string.replace(/\s+/g, " "); // string = " A string with many spaces. "
Notice that any double, triple, etc. space is converted into a single space.
It finds any whitespace with two or more spaces and replaces it with a single space.
\s: matches any whitespace in the string.
+: quantifier that matches any whitespace greater than one space
g: modifier that specifies a global match. So all matches are found (not just the first).