当needle是变量时替换全局

时间:2012-04-30 13:51:42

标签: javascript regex

当要更换的针是变量时,如何使用全局修改器?

var needle = '/'+key+'/g';
str = str.replace(needle, trans[key]);

1 个答案:

答案 0 :(得分:3)

为此换一针:

var needle = new RegExp(key, "g");