Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
22
rated 0 times [  28] [ 6]  / answers: 1 / hits: 23038  / 13 Years ago, thu, november 3, 2011, 12:00:00

I want to combine all them expressions into one and haven't got a clue how to do it, it needs to remove the end white-space and remove the beginning white-space but shorten white-space between two words to only one (if there's more than one). Thanks



var _str = document.contact_form.contact_name.value;
name_str = _str.replace(/s+/g,' ');
str_name = name_str.replace(/s+$/g,'');
name = str_name.replace(/^s+/g,'');
document.contact_form.contact_name.value = name;

More From » string

 Answers
2

You can combine the second two into a single regular expression:



name = _str.replace(/s+/g, ' ').replace(/^s+|s+$/g, '');


You could also look at jQuery's trim method.




Description: Remove the whitespace from the beginning and end of a string.



[#89321] Tuesday, November 1, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cameron

Total Points: 591
Total Questions: 112
Total Answers: 88

Location: Botswana
Member since Sat, Jan 7, 2023
1 Year ago
;