javascript 中的trim實現

<script>
String.prototype.trim= function() 

    // 用正則表達式將前後空格 
    // 用空字符串替代。 
    return this.replace(/(^/s*)|(/s*$)/g, ""); 
}


// 有空格的字符串 
var s = "    leading and trailing spaces     "; 

// 顯示 "    leading and trailing spaces     (35)" 
window.alert(s + " (" + s.length + ")"); 

// 刪除前後空格 
s = s.trim(); 
// 顯示"leading and trailing spaces (27)" 
window.alert(s + " (" + s.length + ")");

發佈了45 篇原創文章 · 獲贊 3 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章