javascript的trim方法

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,"");
}

Posted in  javascript


blog comments powered by Disqus
Fork me on GitHub