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
Want to share an example here:
I want to replace a string like: hi[var1][var2]
to hi[newVar][var2]
.
and var1
are dynamic generated in the page.
so I had to use:
var regex = new RegExp("\\\\["+var1+"\\\\]",'ig');
mystring.replace(regex,'[newVar]');
This works pretty good to me. in case anyone need this like me. The reason I have to go with [] is var1 might be a very easy pattern itself, adding the [] would be much accurate.