In my application i want to remove the unwanted space from starting and ending.And also if there have more than space between words also remove them leaving one space. How can i do this?
3 Answers
Try this:
//remove leading spaces
content = content.replace(/^\s+/g,'');
//remove trailing spaces
content = content.replace(/\s+$/g,'');
//remove whitespaces in the middle
content = content.replace(/\s+/g,' ');
1 Comment
Kichu
when i using this an error occurs "x.rep is undefined" x is my variable
This should work to remove leading and trailing spaces, and reduce two or more spaces to a single space:
str.replace(/^\s+|\s+$/, '').replace('/\s\s+/', ' ')
1 Comment
Max Yari
nah, removes only leading one