我想使用JavaScript替换两个或多个空格的出现,只使用一个空格字符(此代码将作为Chrome扩展程序运行)。
我想使用JavaScript替换两个或多个空格的出现,只使用一个空格字符(此代码将作为Chrome扩展程序运行)。
" this is tooo spaced ".replace(/\s+/g, " ");
返回值
" this is tooo spaced "
您可以使用以下方式同时执行:
"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' );
/\s{2,}/g
更有意义(尽管你的仍然有效)。 - user1385191