正則 捕獲 exec 方法封裝

 掛載到 RegExp 原型對象上 

使用方式:

let reg = /\d+/g
reg.execAll(str)
~function () {
      function execAll (str = '') {
        let list = [],temp = null
        temp = this.exec(str)
        // 是否使用了 g
        if (!this.global) return temp ? temp[0] : []
        while (temp) {
          list.push(temp[0])
          temp = this.exec(str)
        }
        return list
      }
      RegExp.prototype.execAll = execAll
    }()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章