This almost has the answer…
How do you use a variable in a regular expression?
I need to know if I can use variables instead of hardcode in the regex?
str1 = str1.replace(/abcdef/g, "stuvwxyz");
can I use variables instead of /abcdef/g and “stuvwxyz”
Of course that you can, every single bit of this can be dynamic:
Checkout the docs as well.