去除空格的js 和 使用正…

1.String.prototype.Trim = function()
{
    returnthis.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function()
{
    returnthis.replace(/(^\s*)/g, "");
}
String.prototype.Rtrim = function()
{
    returnthis.replace(/(\s*$)/g, "");
}

2.全文匹配替換
var regExp = new RegExp("需要被替換的字符',"g")
var text = "…………";
text = text.replace(regExp , "替換的字符"); 

3.方案
String .prototype.trim = function(){
   var matches =this.match(/^[ \t\n\r]+/);
   var prefixLength =(matches == null) ? 0:matches[0].length;
   matches = this.match(/[\t\r\n]+$/);
   var suffixLength =(matches == null) ? 0:matches[0].length;
   returnthis.slice(prefixLength,this.length-suffixLength);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章