replace(/\s+/g,' ');
将所有空格减少为一个空格,包括换行符。有时我有超过 1 个空格,我想将其减少到 1 个,保留新行、换行。
最佳答案
您应该使用:
string = string.replace(/ {2,}/g, ' ');
\s
匹配所有空格,包括换行符。
或使用前瞻:
string = string.replace(/ +(?= )/g, '');
关于javascript - 如何减少除换行符之外的额外空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20433154/