正则表达式删除所有白色景观,除了单词之间的一个

Regex to remove all whitescape except one between words?

本文关键字:之间 一个 单词 删除 白色 景观 正则表达式      更新时间:2023-09-26

标题很简单,真的。

是否有一个正则表达式可以删除除单词之间的空格之外的所有空格。

所以

"  Hello. How   are  you     today?    "

会成为

"Hello. How are you today?"

这将执行您希望的操作:

"  Hello. How   are  you     today?    ".replace(/'s{2,}/g,' ').trim() 

小提琴:http://jsfiddle.net/REAdV/

您可能会发现trim()可能无法在 6/7/8 和一些较旧的浏览器版本上运行,我建议使用.replace(/^'s+|'s+$/g,'')

var str = "    fsdf d34234sf    sdfsdf   "; 
str=str.replace(/^'s+|'s+$/g,'');
returns:fsdf d34234sf sdfsdf
相关文章: