remove trailing spaces in text js code example Example: remove extra space in string javascript newString = string.replace(/\s+/g,' ').trim();