用正则实现trim()清除字符串两端空格


String.prototype.trim1 = function(){
  // return this.replace(/\s/g,""); // 清除所有空格
  return this.replace(/(^\s
)|(\s*$)/g,""); // 清除字符串前后的空格
};
console.log(" hello word ".trim1()) // "hello word"

相关