[英]How to remove leading and trailing white spaces from a given html string?
[英]How do I remove trailing white spaces but not white spaces within a string (nor at the beginning)?
我有不同長度的字符串,通常后面跟空格(根據字符串的不同而不同)。
即-字符串總是20個字符長
var data = "DUR IT R4356 " //with 8 trailing
或字符串可能是
var data = "11& 444 DTF# 5676 " //with 3 trailing
擺脫那些尾隨空格的最佳方法是什么?
我在想一些JS函數轉到最后一個不是空格的字符,然后用空字符串替換所有空格?
有什么建議么? 如果jQuery對此更好,我也很樂意...
謝謝。
您可以使用以下有用的修整功能:
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+$/,"");
}
例如
alert("11& 444 DTF# 5676 ".rtrim());
data = data.replace(/\s+$/, "");
\\s
空間 +
-一個或多個 您是否嘗試過使用$.trim()
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.