Javascript 用$.Trm消除字符串中间的空格? 我想删除一个字符串中间的空间,用 $.Times()/:例如: console.log($.trim("hello, how are you? "));

Javascript 用$.Trm消除字符串中间的空格? 我想删除一个字符串中间的空间,用 $.Times()/:例如: console.log($.trim("hello, how are you? "));,javascript,jquery,Javascript,Jquery,我得到: hello, how are you? 我怎样才能得到它 hello, how are you? 谢谢。一个解决方案是使用javascriptreplace 我建议您使用regex var str=“你好,你好吗?”; str=str.replace(/\s\s+/g',); console.log(str)您可以使用正则表达式将所有连续空格\s\s+替换为字符串',这将消除空格并仅保留一个空格,然后$。修剪将处理起始和/或结束空格: var string =

我得到:

hello,           how are you?
我怎样才能得到它

hello, how are you?

谢谢。

一个解决方案是使用javascript
replace

我建议您使用
regex

var str=“你好,你好吗?”;
str=str.replace(/\s\s+/g',);

console.log(str)
您可以使用正则表达式将所有连续空格
\s\s+
替换为字符串
'
,这将消除空格并仅保留一个空格,然后
$。修剪将处理起始和/或结束空格:

var string = "hello,           how are you?       ";
console.log($.trim(string.replace(/\s\s+/g, ' ')));

您检查过这个==>谢谢,祝大家新年快乐!