Adding modifiers to an existing regular expression

前端 未结 4 921
一生所求
一生所求 2021-01-04 01:51

I have a bunch of regular expressions like lower = /[a-z]/ Later in my program i need to use this as /[a-z]/g ie. i need to add the \'global\' modifier later. So how to

4条回答
  •  青春惊慌失措
    2021-01-04 02:52

    You can write a method for it-

    RegExp.prototype.reflag= function(flags){
        return RegExp(this.source, flags);
    }
    

提交回复
热议问题