Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs request.url.match(regexp) seems to return a substring of the match in addition to the match

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

like image 476
Newbie Avatar asked Mar 18 '13 21:03

Newbie


People also ask

Which of the following RegExp methods will return a true if a pattern match is present in a string?

test(str) The method regexp. test(str) looks for a match and returns true/false whether it exists.

Which method of RegExp class takes a string argument and returns True False depending on whether the string contains a match of the regular expression *?

The test() method is a RegExp expression method. It searches a string for a pattern, and returns true or false, depending on the result.

How do you match expressions in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What is the regular expression function to match all occurrences of a string?

Regular expressions are used with the RegExp methods test() and exec() and with the String methods match() , replace() , search() , and split() . Executes a search for a match in a string. It returns an array of information or null on a mismatch.


1 Answers

Take 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.
like image 180
Fabrício Matté Avatar answered Sep 29 '22 07:09

Fabrício Matté