Reputation: 15715
Why does this code (here's the JSBin):
var text = "T: 01202 870738";
var regex1 = /T: (.*)/;
var matches1 = text.match(regex1);
for(var i = 0; i < matches1.length; i++) {
log("[" + i + "]: " + matches1[i]);
}
logs this:
[0]: T: 01202 870738
[1]: 01202 870738
and this code (note I've added the g
option):
var regex2 = /T: (.*)/g;
var matches2 = text.match(regex2);
for(var i = 0; i < matches2.length; i++) {
log("[" + i + "]: " + matches2[i]);
}
logs this:
[0]: T: 01202 870738
I actually don't even understand why is the first code logging 01202 870738
as the second match. How is that a match for /T: (.*)/
if it doesn't include a T:
?
Upvotes: 1
Views: 70
Reputation: 456
What throws you off is the different behavior you get from a regex with and without the g
flag. Calling String.match
with a g
flagged regex will return an array of all instances of the pattern within the String
object. For example, the expression:
"Hello World!".match(/l/g);
Will return this array:
["l", "l", "l"]
However, calling the same function without the g
flag will return an array whose first element is the matched pattern. Any element thereafter will match each expression within parentheses. For example, the expression:
"Hello World!".match(/(Hell)o World(!)/);
Will conveniently return this array:
["Hello World!", "Hell", "!"]
Upvotes: 1
Reputation: 224867
The second one is a global regular expression, so the array returned is a list of all the matches for the expression in the string. The first one isn’t, so it’s a list of groups, like you would get from exec
. (Group zero being the entire match, and group one being the only parenthesized... group.)
Upvotes: 3