I’m working on a program that extract information from a large chuck of text using javascript and while looking at a former co-workers code of something similar I found that when you save the result of .exec() and do .index of that variable it gives you the index of that substring in the array.
Example:
var str="I found this for you!";
var pattern=/this/igm;
var result=pattern.exec(str);
document.write("\"This\" Index = " + result.index + ".");
Result:
"This" Index = 8.
When I looked online I found that exec() returns an array and it looks like arrays don’t have an .index property. All my searches of .index seem to come up index().
What is going on here? Why does this work? I’m also wondering if there are some other things I can do related to this (like .lastindex).
Here is a great resource on what
execdoes. It not only returns an array with extra properties likeindex, but it also modifies the regex object that was used.Try this: