由于我同时学习 Javascript 和 Express.js,因此我在发出 get 请求时尝试使用正则表达式
为了让我自己熟悉正则表达式,我使用了这个图表(也在下面复制)
Greedy Reluctant Possessive Meaning
X? X?? X?+ X, once or not at all
X* X*? X*+ X, zero or more times
X+ X+? X++ X, one or more times
X{n} X{n}? X{n}+ X, exactly n times
X{n,} X{n,}? X{n,}+ X, at least n times
X{n,m} X{n,m}? X{n,m}+ X, at least n but not more than m times
我的问题是,如果它只有一个/.
换句话说,它只会匹配默认的 urllocalhost:1337/
app.get(/\/{1}/, function (req, res) {
res.render("index");
});
但是,我上面的当前正则表达式匹配其他路径名(即。localhost:1337/home/login)因为现在我知道它使用贪婪量词
在阅读了更多关于正则表达式的内容后,我将量词更改为所有格。
/\/{1}+/
但是后来快递给了这个错误:
Syntax Error: Invalid Regular Expression: /\/{1}+/: Nothing to Repeat
那么我的正则表达式语法错误吗?