将正则表达式与coffeescript中的模板匹配

时间:2015-09-23 10:36:59

标签: javascript regex coffeescript

我需要匹配这样的事情:

pid = '0000000001'
redirectUrlMatch = /https:\/\/example.com\/products\/.*?\/pages\/(pid)\/redirect\/(pid)/
assert.equal validator.matches(html, redirectUrlMatch), true

但它失败了,因为我认为pid不能用于正则表达式 我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

似乎你想把pid放到正则表达式中,如下所示

/https:\/\/example.com\/products\/.*?\/pages\/(0000000001)\/redirect\/(0000000001)/

你可以这样做:

pid = '0000000001'
var redirectUrlMatch = new RegExp("https:\/\/example.com\/products\/.*?\/pages\/("+pid+")\/redirect\/("+pid+")");