Sometimes I need to use regex with g and sometimes without g.
let setreg = /set (\w+) (.+?)/g;
let setregnog = /set (\w+) (.+?)/;
Is there a way to just add a g to the end of setreg instead of using 2 vars.
Sometimes I need to use regex with g and sometimes without g.
let setreg = /set (\w+) (.+?)/g;
let setregnog = /set (\w+) (.+?)/;
Is there a way to just add a g to the end of setreg instead of using 2 vars.
Yes - you can use the RegExp()
constructor.
let ptn = /foo/;
new RegExp(ptn, 'g'); //<-- with
new RegExp(ptn); //<-- without