Changing the RegExp flags

后端 未结 4 1982
长发绾君心
长发绾君心 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:54

    var globalRegex = new RegExp(needle.source, "g");
    

    Live Demo EDIT: The m was only for the sake of demonstrating that you can set multiple modifiers

    var regex = /find/;
    var other = new RegExp(regex.source, "gm");
    alert(other.global);
    alert(other.multiline);
    

提交回复
热议问题