Passing regex modifier options to RegExp object

后端 未结 6 1773
陌清茗
陌清茗 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:12

    When using the RegExp constructor, you don't need the slashes like you do when using a regexp literal. So:

    new RegExp(keyword, "i");
    

    Note that you pass in the flags in the second parameter. See here for more info.

提交回复
热议问题