I tried a piece of code like this
var match = req.url.match(/^\/user\/(.+)/)
And passed a url like so "___http://someurl/user/jane"
as it turned out match was initialized to an array with the following elements
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.
My experience with JavaScript is minimal and I couldn't find an explanation after some looking around. Appreciate an explanation of this
thanks
I tried a piece of code like this
var match = req.url.match(/^\/user\/(.+)/)
And passed a url like so "___http://someurl/user/jane"
as it turned out match was initialized to an array with the following elements
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.
My experience with JavaScript is minimal and I couldn't find an explanation after some looking around. Appreciate an explanation of this
thanks
Share Improve this question edited Mar 18, 2013 at 21:35 Fabrício Matté 70.2k27 gold badges133 silver badges167 bronze badges asked Mar 18, 2013 at 21:31 NewbieNewbie 5351 gold badge7 silver badges17 bronze badges1 Answer
Reset to default 10Take a look at String.match
, or better, RegExp.exec
which has the same return value as String.match
for a regex without the g
flag:
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 brackets¹ makes a capturing group. If you only need the full match you can use:
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.
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
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.
¹ Is not always the case, there are modifiers that make it a non-capturing group (
?:
), lookaheads, lookbehinds etc. but these are off-topic. You can find more about these in the site linked above.