A
andrewflanders
I have an associative array of keys and values. I want to search a
string for the existance of keys and replace them with the values in
the array. The problem is that some of the keys resemble regular
expressions but they are not so if I build the regular expression with
a constructor it causes errors saying that the regular expression
syntax is bad. I got around this problem by skipping the regular
expression method and passing in the key directly as the first argument
of replace however this means I lose the ability to pass the /g global
switch to the replace function so it only replaces once.
So my question is: is there a way to incorperate a variable into a
regex without enterpreting the variable as a regex itself or
alternately is there a way to make the replacement global without using
a regular expression?
string for the existance of keys and replace them with the values in
the array. The problem is that some of the keys resemble regular
expressions but they are not so if I build the regular expression with
a constructor it causes errors saying that the regular expression
syntax is bad. I got around this problem by skipping the regular
expression method and passing in the key directly as the first argument
of replace however this means I lose the ability to pass the /g global
switch to the replace function so it only replaces once.
So my question is: is there a way to incorperate a variable into a
regex without enterpreting the variable as a regex itself or
alternately is there a way to make the replacement global without using
a regular expression?