Javascript-删除空格-不中断

Javascript-删除空格-不中断,javascript,whitespace,paragraph,Javascript,Whitespace,Paragraph,如何删除字符串中的空格,而不删除其中的分隔符 $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.

如何删除字符串中的空格,而不删除其中的分隔符

$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',);
//或者$new=$new.replace(/(\s(?=\s))+/g',);
document.getElementById('result').innerHTML=$new

这是一个测试。
请只删除
空间不是
里面破了

更好的演示应该使用
pre
tag;)可以,但在常规Express中使用空格不是ISOConform@dazzafact我尝试了另一种方法,ISO符合吗?这与所问的完全相反。因为您将丢失
换行符
(因为
s
)-换行符是需要的,多个空格不是。现在它什么都不起作用。@Juhana:不,它没有。这是一种非常常见的javascript技术。如果用相同数量的空格替换所有空格,则只剩下完全相同的字符串。继续尝试。为什么要使用
\s+
?你知道这是什么意思吗?
$new = $new.split(/ +/).join(' ');