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
时,它失败了<代码>无法获取。请看我的更新上面。