封装一个exec方式:直接把东西都拿到

RegExp.prototype.execAll = function(str){
    //若是加全局的g就会出现死循环,必须使用global判断下 默认是true
    let _this = this;
    if(!_this.global){
        let s = /^\/(.+)\/$/.exec(_this + '')[1];
        _this = new RegExp(s,'g')
    }
    let res = _this.exec(str);
    let ary = [];
    while(res){
        ary.push(res);
        res =_this.exec(str)
    }
    return ary
}
var str = '上课1123好好1521学习1222';
var reg = /\d+/;
console.log(reg.execAll(str));
复制代码
相关文章
相关标签/搜索