I want remove the space in the middle of a string with $.trim()
for example:
console.log($.trim("hello, how are you? "));
I get:
hello, how are you?
how can I get
hello, how are you?
Thanks.
You can use regular expression to replace all consecutive spaces \s\s+
with a single space as string ' '
, this will eliminate the spaces and keep only one space, then the $.trim
will take care of the starting and/or ending spaces:
var string = "hello, how are you? ";
console.log($.trim(string.replace(/\s\s+/g, ' ')));
One solution is to use javascript replace
.
I recommend you to use regex
.
var str="hello, how are you? ";
str=str.replace( /\s\s+/g, ' ' );
console.log(str);
Another easy way is to use .join()
method.
var str="hello, how are you? ";
str=str.split(/\s+/).join(' ');
console.log(str);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With