I want to replace the "3" into "5". When it is using static it's working fine but when I use it through variable var allvar= '"3"';
it's not working fine.
Here is the jsfiddle link
I want to replace the "3" into "5". When it is using static it's working fine but when I use it through variable var allvar= '"3"';
it's not working fine.
Here is the jsfiddle link
new RegExp( /[allvar]+/g );
will construct a regular expression matching all uninterrupted sequences of one or more characters from the set a, l, v, a, r
.
To construct a regular expression from a variable, you can do this:
new RegExp(allvar, 'g')
It would also be good to escape characters with special meaning to RegExp, unless you intend for allvar
to contain regexp source. Unfortunately, RegExp.escape
is still not in the language, so one would use a workaround.
new RegExp(escapeRegExp(allvar), 'g')