Eliminar espacio adicional en la cadena JS
newString = string.replace(/\s+/g,' ').trim();
Expensive Eel
newString = string.replace(/\s+/g,' ').trim();
const sentence = ' My string with a lot of Whitespace. '.replace(/\s+/g, ' ').trim()
// 'My string with a lot of Whitespace.'
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!