用单个空格替换多个空格的正则表达式给出如下的字符串:"The dog has a long tail, and it is RED!"什么样的jQuery或JavaScript魔法可以将空间保持在一个最大空间?目标:"The dog has a long tail, and it is RED!"
3 回答

阿波罗的战车
TA贡献1862条经验 获得超6个赞
\s\s+
' '
:
string = string.replace(/\s\s+/g, ' ');
string = string.replace(/ +/g, ' ');

芜湖不芜
TA贡献1796条经验 获得超7个赞
str.replace( / +/g, ' ' ) -> 380msstr.replace( /\s\s+/g, ' ' ) -> 390msstr.replace( / {2,}/g, ' ' ) -> 470msstr.replace( / +/g, ' ' ) -> 790msstr.replace( / +(?= )/g, ' ') -> 3250ms

慕妹3242003
TA贡献1824条经验 获得超6个赞
var str = "The dog has a long tail, and it is RED!";str = str.replace(/ {2,}/g,' ');
编辑:
str = str.replace(/\s{2,}/g,' ');
添加回答
举报
0/150
提交
取消