js method string remove extra spaces

JavaScript
str.replace(/\s+/g, '')const sentence = '    My string with a    lot   of Whitespace.  '.replace(/\s+/g, ' ').trim()

// 'My string with a lot of Whitespace.'
str = str.replace(/\s/g, '');
Source

Also in JavaScript: