在Javascript中只匹配WhitePacs而不匹配tab、回车或换行符的正则表达式

在Javascript中只匹配WhitePacs而不匹配tab、回车或换行符的正则表达式,javascript,regex,Javascript,Regex,我想格式化一个不带多个空格的字符串。字符串可以有制表符、回车符或换行符 例1: 你好 世界 expact结果:hello 世界 示例2:hello world 预期结果:“你好,世界” const formatString=(s)=>{ 常数修剪=s.修剪(); const formatted=trimmed.match(/\s/g) 返回s.trim()。替换(/\s+/g') } const str=`你好 世界` const result=formatString(str) cons

我想格式化一个不带多个空格的字符串。字符串可以有制表符、回车符或换行符

例1: 你好 世界 expact结果:
hello
世界

示例2:
hello world
预期结果:“你好,世界”

const formatString=(s)=>{
常数修剪=s.修剪();
const formatted=trimmed.match(/\s/g)
返回s.trim()。替换(/\s+/g')
}
const str=`你好
世界`
const result=formatString(str)
console.log(结果)
您可以使用空格(精确的sapce)。容易的

const formatString=(s)=>{
返回s.replace(+/+/g,“”);
};
让str=`你好
世界`;
const result=formatString(str);
控制台日志(结果);
str=`helloworld`;
log(formatString(str))