nodejs request.url.match(regexp) 似乎除了匹配之外还返回匹配的子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15487334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nodejs request.url.match(regexp) seems to return a substring of the match in addition to the match
提问by Newbie
I tried a piece of code like this
我试过一段这样的代码
var match = req.url.match(/^\/user\/(.+)/)
And passed a url like so "___http://someurl/user/jane"
并通过这样的网址 "___http://someurl/user/jane"
as it turned out match was initialized to an array with the following elements
结果是 match 被初始化为一个包含以下元素的数组
match[0]='/user/jane'
match[1]='jane'
I would have expected a single element i.e. the first element in match[0]. why was the second string returned -it doesn't seem to match the regex.
我本来希望有一个元素,即match[0]. 为什么返回第二个字符串 - 它似乎与正则表达式不匹配。
My experience with JavaScript is minimal and I couldn't find an explanation after some looking around. Appreciate an explanation of this
我在 JavaScript 方面的经验很少,在环顾四周后我找不到解释。感谢对此的解释
thanks
谢谢
回答by Fabrício Matté
Take a look at String.match, or better, RegExp.execwhich has the same return value as String.matchfor a regex without the gflag:
看看String.match,或者更好,RegExp.exec它与String.match没有g标志的正则表达式具有相同的返回值:
The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.
返回的数组将匹配的文本作为第一项,然后是每个匹配的捕获括号的一项,其中包含捕获的文本。
That is, a group between round brackets1 makes a capturing group. If you only need the full match you can use:
即,圆括号 1 之间的组构成捕获组。如果您只需要完整匹配,您可以使用:
var match = req.url.match(/^\/user\/.+/)[0];
console.log(match); //logs: "/user/jane"
This will extract the whole match (at index 0) returning it to the variable match, the rest of the array is discarded.
这将提取整个匹配项(在 index 处0),将其返回给变量match,数组的其余部分将被丢弃。
Note: If the regex may not match, you should test to see if it returns a match before extracting the full match to prevent against errors:
注意:如果正则表达式可能不匹配,您应该在提取完整匹配之前测试它是否返回匹配以防止错误:
var match = req.url.match(/^\/user\/.+/);
if (match !== null) {
match = match[0];
console.log(match); //"/user/jane"
} else {
console.log("no match");
}
Here's a live demo for fiddling around: jsFiddle
这是一个摆弄的现场演示:jsFiddle
I've removed the capturing group as it wouldn't make a difference in this case. Doesn't actually matter, just a micro-optimization.
我已经删除了捕获组,因为在这种情况下它不会产生任何影响。实际上并不重要,只是微优化。
You can read more about Regular Expressions' capturing groups/backreferences here.
1 情况并非总是如此,有些修饰符使其成为非捕获组 (
?:?:)、前瞻、后视等,但这些都是题外话。您可以在上面链接的网站中找到更多关于这些的信息。
