变量内部模式

时间:2011-09-20 11:30:08

标签: javascript regex dynamic

var seperator = ',', group = 'red, blue';
//group.search(seperator/g) - g is not defined
group.search(/seperator/g) // looks for "seperator"
group.search('/' + seperator + '/g') // doesn't seem to find my "seperator"

由此我没有想法......我如何在表达中得到seperator

提前致谢!

1 个答案:

答案 0 :(得分:5)

您需要创建新的regexp对象

var test = new RegExp(seperator, 'g');
group.search(test)