简体   繁体   English

Express.js路由器中的正则表达式

[英]Regex in Express.js router

I'm trying to find some documentation about regex in Express, but the information in Express API is very sparse. 我试图在Express中找到一些有关正则表达式的文档,但是Express API中的信息很少。 I'm trying to do a regex matching objectID. 我正在尝试做一个正则表达式匹配的objectID。 This example about regex is given in the Express documentation. Express文档中提供了有关正则表达式的示例。

router.get(/^\/commits\/(\w+)(?:\.\.(\w+))?$/, function(req, res){ ... });    

I have tried the following in my router, and it seems to work fine. 我在路由器中尝试了以下操作,但似乎工作正常。

client.get('/staff/:id([0-9a-fA-F]{24})', function(req, res) { ... }); 

But there are some differences that i can't figure out... 但是有些差异我无法弄清楚...

  • My example is surrounded in ' , the example in the API is not. 我的示例用'包围,而API中的示例则没有。 What does this mean? 这是什么意思? Is my expression a string and not a regex? 我的表达式是字符串而不是正则表达式吗?
  • I dont use: /^ or ?$/ . 我不使用: /^?$/ Not knowing much about regex I guess this is some kind of anchors. 对正则表达式了解不多,我想这是某种锚。 Do i need this? 我需要这个吗?
  • I dont escape \\ the first part of my URL /staff/:id . 我没有逃避\\ URL的第一部分/staff/:id Is this something i should do? 这是我应该做的事吗?

Also, does anyone know about a extensive resource for reading about regex in Express routers... or parameter options or whatever it is I'm doing above... 另外,没有人知道广泛的资源来阅读Express路由器中的正则表达式...或参数选项或我在上面做的任何事情...

It is worth reading the source code for the route matching, but here is the short form. 值得阅读路由匹配的源代码,但这是缩写形式。

  • 'abc' is a JS string; 'abc'是一个JS字符串; /abc/ is a JS regex. /abc/是JS正则表达式。 The string can be used to create a regex, which is what you are doing. 该字符串可用于创建正则表达式,这就是您正在做的事情。 So both are valid. 因此,两者都是有效的。 To see the difference, try doing var re = /abc/ and var re = new RegExp('abc') 若要查看区别,请尝试执行var re = /abc/var re = new RegExp('abc')
  • ^ is a starting anchor while $ is an ending anchor. ^是开始锚点,而$是结束锚点。 ^abc will match "abc", "abcde" but not "zabcd" because it needs to start with "abc". ^abc将匹配“ abc”,“ abcde”,但不匹配“ zabcd”,因为它需要以“ abc” 开头 abc$ will match "abc", "zabc" but not "abcd", because it needs to end with "abc". abc$将匹配“ abc”,“ zabc”,但不匹配“ abcd”,因为它需要以“ abc”结尾。
  • when you use an actual RegExp expression starting with / and ending with / , if you want to use slashes in your RegExp, you need to escape them, else how will the interpreter know that this is a slash character and not the end of your RegExp? 当您使用一个实际的RegExp表达式开始/和结尾/ ,如果你想在你的正则表达式用斜线,你需要逃避他们,要不然怎么会解释知道这是一个斜线字符,而不是你的正则表达式的结束? If you are using a string, it knows it is not, and so you do not need to escape. 如果您使用的是字符串,它会知道不是,因此您无需转义。

For the last, try this: 最后,请尝试以下操作:

"abc".match(/abc/);  // works
"abc".match('/abc/');  // fails because there are no slashes
"/abc/".match('/abc/');  // works
"/abc/".match(/abc/);  // works because "abc" is in there

Of course, you can escape the slashes, if you so choose 当然,如果您愿意的话,您可以逃脱斜线

"/abc/".match('\/abc\/');  // works
"abc".match('\/abc\/');  // fails

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM