【发布时间】:2011-10-04 16:54:46
【问题描述】:
我想使用 JavaScript 用一个空白字符替换两次或多次出现的空白(此代码将位于 chrome 扩展中)。
【问题讨论】:
标签: javascript regex string
我想使用 JavaScript 用一个空白字符替换两次或多次出现的空白(此代码将位于 chrome 扩展中)。
【问题讨论】:
标签: javascript regex string
" this is tooo spaced ".replace(/\s+/g, " ");
返回
" this is tooo spaced "
【讨论】:
/\s{2,}/g 会更有意义(尽管你的仍然有效)。
你可以同时做到这两点:
"str str\t\t\tstr".replace(/(\s)+/g, "$1");
【讨论】:
对于空格:
'test with spaces'.replace( /\s+/g, ' ' );
对于标签:
'test\t\t\twith\t\t\ttabs'.replace( /\t+/g, '\t' );
【讨论】: