[英]How do I remove trailing white spaces but not white spaces within a string (nor at the beginning)?
I have a string of varying length and is usually followed by white spaces (of varying length based on he string). 我有不同长度的字符串,通常后面跟空格(根据字符串的不同而不同)。
ie - the string is always 20 characters long 即-字符串总是20个字符长
var data = "DUR IT R4356 " //with 8 trailing
or the string could be 或字符串可能是
var data = "11& 444 DTF# 5676 " //with 3 trailing
What is the best way to get rid of those trailing white spaces? 摆脱那些尾随空格的最佳方法是什么?
I was thinking some JS function that goes to the last character that is not a white space, then replace all the white spaces with empty string ? 我在想一些JS函数转到最后一个不是空格的字符,然后用空字符串替换所有空格?
Any suggestions? 有什么建议么? If jQuery is better for this I am open to that as well... 如果jQuery对此更好,我也很乐意...
Thanks. 谢谢。
Here are some useful trimming functions you can use: 您可以使用以下有用的修整功能:
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
return this.replace(/\s+$/,"");
}
eg 例如
alert("11& 444 DTF# 5676 ".rtrim());
data = data.replace(/\s+$/, "");
\\s
- space \\s
空间 +
- one or more +
-一个或多个 您是否尝试过使用$.trim()
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.