在coffeescript中匹配regex和模板

Match regex with template in coffeescript

本文关键字:regex coffeescript      更新时间:2023-09-26

我需要匹配这样的东西:

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

但它失败了,因为我认为pid不能被放置到正则表达式我该怎么做呢?

似乎您想将pid放入正则表达式中,如下所示

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

你可以这样做:

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