如何使用正则表达式删除双空格字符? [英] How to remove double white space character using regexp?
本文介绍了如何使用正则表达式删除双空格字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
输入:
". . . . ."
预期输出:
". . . . ."
推荐答案
text = text.replace(/\s{2,}/g, ' ');
-
\ s
将占用所有空格,包括换行符,因此您可以将其更改为/{2,}/g
. -
{2,}
需要两个或更多.与\ s +
不同,这不会用单个空格替换单个空格.(有点优化,但通常会有所不同) - 最后,JavaScript中需要
g
标志,否则它只会更改第一个空格块,而不会更改所有空格. \s
will take all spaces, including new lines, so you may change that to/ {2,}/g
.{2,}
takes two or more. Unlike\s+
, this will not replace a single space with a single space. (a bit of an optimization, but it usually makes a differance)- Finally, the
g
flag is needed in JavaScript, or it will only change the first block of spaces, and not all of them.
这篇关于如何使用正则表达式删除双空格字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文