Changing the RegExp flags

后端 未结 4 1979
长发绾君心
长发绾君心 2020-12-16 12:57

So basically I wrote myself this function so as to be able to count the number of occurances of a Substring in a String:

String.prototype.numberOf = function         


        
4条回答
  •  一生所求
    2020-12-16 13:53

    There isn't much you can do but I highly recommend you avoid using eval. You can extend the RegExp prototype to help you out.

    RegExp.prototype.flags = function () {
        return (this.ignoreCase ? "i" : "")
            + (this.multiline ? "m" : "")
            + (this.global ? "g" : "");
    };
    
    var reg1 = /AAA/i;
    var reg2 = new RegExp(reg1.source, reg1.flags() + 'g');
    

提交回复
热议问题