25

问答题 25 /85

请给JavaScript的String 原生对象添加一个名为trim 的原型方法,用于截取空白字符。要求
alert(" taobao".trim());     // 输出 "taobao"
alert(" taobao ".trim());    // 输出 "taobao"

参考答案

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