index method or property
on page 156 at the bottom of the page I am working with this code:
var text = "cat, bat, sat, fat";
var pattern = /.at/;
//same as pattern.exec(text)
var matches = text.match(pattern);
alert(matches.index); // this line here
alert(matches[0]);
alert(pattern.lastIndex);
I had a question about the .index above.
Here "matches" appears to be a string (if there is only one match returned).
When I changed pattern to be "/.at/g" then multiple strings are returned and then I can access matches[1] which is "bat".
However in that case matches.index returns undefined instead of 0.
As an aside I do not know why the author included the last alert.
|