Javascript - 删除 space - 不中断

Javascript - Remove space- not breaks

如何删除字符串中的 space,而不删除其中的分隔符?

$new=$new.replace(/\s+/g,' '); //not working correct


this       is a test.
please    remove only   the
Space not
the       breaks inside


this is a test.
please remove only the
Space not
the breaks

只需使用/ +/g:

$new = document.getElementById('content').innerHTML;

$new = $new.replace(/ +/g,' ');
// or $new = $new.replace(/(\s(?=\s))+/g,'');
 
document.getElementById('result').innerHTML = $new;
<pre id="content">
this       is a test.
please    remove only   the
Space not
the       breaks inside
</pre>

<pre id="result">
</pre>

$new = $new.split(/ +/).join(' ');