To replace multiple spaces with a single space in JavaScript, you can use regular expressions and the `replace` method.
In this example,
str
with multiple spaces.replace
method with a regular expression /\s+/g
to replace multiple spaces with a single space.str
.let str = "This is a test string";
let modifiedStr = str.replace(/\s+/g, ' ');
console.log(modifiedStr);
This is a test string
In this tutorial, we learned How to Replace multiple Spaces with a Single Space in String in JavaScript language with well detailed examples.