對javascript中String類型的拓展

jsopen 9年前發布 | 1K 次閱讀 JavaScript

/*

  • @desccrition: 對String類型去除空格的拓展
  • @dir : 被去除空格所在的位置
  • @test: ie6-9 chrome firefox */ String.prototype.trim = function(dir){ switch (dir) {
    case 0 : //去左邊的空格
      return this.replace(/(^\s*)/g,'');
      break;
    case 1 : //去右邊的空格
      return this.replace(/(\s*$)/g,'');
      break;
    case 2 : //去掉所有的空格
      return this.replace(/(\s*)/g,'');
      break;
    default : //去掉兩邊的空格
      return this.replace(/(^\s*)|(\s*$)/g,'');
    
    } }</pre>
 本文由用戶 jsopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!