Javascript - 删除空格 - 而不是中断

Javascript - Remove space- not breaks

本文关键字:中断 空格 删除 Javascript      更新时间:2023-09-26

如何在不删除内部分隔符的情况下删除字符串中的空格?

$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(' ');