Suppose below is the string which needs to be formatted by removing the whitespaces.
const string = 'string having white spaces ';
The regular expression we are going to use is
So, the full code that you can use is
const string = 'string having white spaces '; const formattedString = string.replace(/\s/g,'');
Note the new formatted string needs to be assigned to a different variable to be used with formatted string.