const removeExtraSpaces = (string) => { const newText = string .replace(/s+/g, " ") .replace(/^s+|s+$/g, "") .replace(/ +(W)/g, "$1"); return newText }; console.log(removeExtraSpaces(" Hello World!")) //Hello World!