How to remove duplicate white spaces in a string?
Possible Duplicate:
Removing whitespace from string in JavaScript
I have used trim function to remove the white spaces at the beginning and end of the sentences. If there is to much white spaces in between words in a sentence, is there any method to trim?
for example
"abc def. fds sdff."
var str = 'asdads adasd adsd';
str = str.replace(/s+/g, ' ');
I think what you're looking for is JavaScript's string.replace() method.
If you want all whitespace removed, use this:
"abc def. fds sdff.".replace(/s/g, '');
Returns: "abcdef.fdssdff."
If you want only double-spaces removed, use:
"abc def. fds sdff.".replace(/ss/g, ' ');
Returns: "abc def. fds sdff."
If you want the space left after a period, use:
"abc def. fds sdff.".replace(/[^.]s/g, '')
Returns: "abcdef. fdssdff."
链接地址: http://www.djcxy.com/p/27512.html
上一篇: Javascript:如何检查一个字符串是否为空?
下一篇: 如何删除字符串中的重复空格?