用一个空白字符替换连续的空白

Replacing consecutive whitespace with one whitespace character

本文关键字:空白 字符 替换 连续 一个      更新时间:2023-09-26

我想用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' );