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
test(str) The method regexp. test(str) looks for a match and returns true/false whether it exists.
The test() method is a RegExp expression method. It searches a string for a pattern, and returns true or false, depending on the result.
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).
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.
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.
?:
), lookaheads, lookbehinds etc. but these are off-topic. You can find more about these in the site linked above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With