开发者

variable inside pattern [duplicate]

This开发者_C百科 question already has answers here: How do I perform a Javascript match with a pattern that depends on a variable? (3 answers) Closed 9 years ago.
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"

And with that I'm out of ideas... How do I get my seperator within expression?

Thanks in advance!


You need to create new regexp Object

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜