Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
115
rated 0 times [  121] [ 6]  / answers: 1 / hits: 15701  / 8 Years ago, mon, january 2, 2017, 12:00:00

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.


More From » jquery

 Answers
0

You can use regular expression to replace all consecutive spaces ss+ 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(/ss+/g, ' ')));

[#59489] Friday, December 30, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
conorm

Total Points: 339
Total Questions: 104
Total Answers: 104

Location: Angola
Member since Tue, May 5, 2020
4 Years ago
;