Express node.js使用新的RegExp快速路由

Express node.js使用新的RegExp快速路由,express,Express,这个路由器为什么工作 app.get(/regularExpression/, function (req, res, next) { }); 但这不起作用无法获取 var regularExpressionString = "regularExpressionString"; var regularExpression = new RegExp(regularExpressionString); app.get(regullarExpression, function (req,

这个路由器为什么工作

app.get(/regularExpression/, function (req, res, next) {
});
但这不起作用
无法获取

var regularExpressionString = "regularExpressionString";    
var regularExpression = new RegExp(regularExpressionString);

app.get(regullarExpression, function (req, res, next) {
});
我需要第二个变体,因为我想根据DRY原则重用路由器字符串部分,而无需重复代码。但是当我使用
/regularpression/
I
/regularpressionstring1+regularpressionstring2/

那么如何在node.js express regex路由中连接字符串

更新 如果我不使用特殊符号,如
\d
\w

例如,此模式可以工作:

regex = new RegExp('/(32)teeth')
但这并不是:

regex = new RegExp('/(\d+)teeth')

在使用
RegExp
构造函数时,应转义
\
符号,因此:

regex = new RegExp('/(\\d+)teeth')

请参见

第二种变体也应适用。确保您在浏览器中导航到
/regularExpressionString
,而不仅仅是
/regularExpression
@CuriousGuy谢谢。像
newregexp('/regularExpressionString')
这样的简单正则表达式确实有效。但是当我尝试使用
\d
\s
时,它失败了<代码>无法获取。请看我的更新上面。