【问题标题】:variable inside pattern [duplicate]模式内的变量[重复]
【发布时间】:2011-09-20 11:30:07
【问题描述】:
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

提前致谢!

【问题讨论】:

标签: javascript regex dynamic


【解决方案1】:

你需要创建新的正则表达式对象

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-03-05
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
  • 2020-05-11
  • 2017-12-18
相关资源
最近更新 更多