js method string remove extra spaces code example
Example 1: remove spaces in a string js
str.replace(/\s+/g, '')
Example 2: js method string remove extra spaces
const sentence = ' My string with a lot of Whitespace. '.replace(/\s+/g, ' ').trim()
// 'My string with a lot of Whitespace.'
Example 3: javascript remove all spaces from string
str = str.replace(/\s/g, '');