javascript的trim方法

20 Nov 2008

javascript的String类是没有trim方法的,不过我们可以通过String的replace方法来模拟

String.prototype.trim = function() {
    return this.replace(/^\s+/g,"").replace(/\s+$/g,"");
}
String.prototype.ltrim = function() {
   return this.replace(/^\s+/g,"");
}
String.prototype.rtrim = function() {
   return this.replace(/\s+$/g,"");
}

Tags 


blog comments powered by Disqus