开发者

variable inside pattern [duplicate]

开发者 https://www.devze.com 2023-04-05 22:32 出处:网络
This开发者_C百科 question already has answers here: How do I perform a Javascript match with a pattern that depends on a variable?
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

精彩评论

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

关注公众号