请给JavaScript的String 原生对象添加一个名为trim 的原型方法,用于截取空白字符。要求
alert(" taobao".trim()); // 输出 "taobao"
alert(" taobao ".trim()); // 输出 "taobao"
String.prototype.trim = function() {
return this.replace(/^\s+$/g, "");
};
console.log(" taobao".trim()); // 输出 "taobao"
console.log(" taobao ".trim()); // 输出 "taobao"
|