Passing regex modifier options to RegExp object

后端 未结 6 1793
陌清茗
陌清茗 2020-11-27 06:56

I am trying to create something similar to this:

var regexp_loc = /e/i;

except I want the regexp to be dependent on a string, so I tried to

6条回答
  •  鱼传尺愫
    2020-11-27 07:36

    You need to convert RegExp, you actually can create a simple function to do it for you:

    function toReg(str) {
      if(!str || typeof str !== "string") {
        return;
      }
      return new RegExp(str, "i");
    }
    

    and call it like:

    toReg("something")
    

提交回复
热议问题