信息交流、传播、提炼

nice to meet you

博客园 首页 新随笔 联系 订阅 管理

1.prototype
String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function()
{
    return this.replace(/(^\s*)/g, "");
}
String.prototype.Rtrim = function()
{
    return this.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;
   return this.slice(prefixLength,this.length-suffixLength);
}


一般应用:
function strTrim(str){
 return str.replace(/(^\s*)|(\s*$)/g,"");
}

posted on 2011-12-02 16:16  seeyou  阅读(167)  评论(0)    收藏  举报