The RegExp exec method will execute a search for the pattern that it is call off of for the given string, and will return a result array if a match is found, or null if no match is found. In addition regular expression Object instances have a last index property that will be updated each time the exec method is called. So the exec method can be used with a loop of some kind along with the global flag to get all index values of the pattern in a string that is passed as the first argument when calling exec.
So because each time the exec method is called an object with the index value of the beginning of a pattern match is obtained, the exec method is great for creating an array of Objects with detailed information with each pattern match. So this makes the exec method a great solution for projects that are parsers, or any kind of project where an array of detailed pattern matching data is needed.
Lets start out with just a basic example of this exec method of the RegExp Class just to get the general idea of how it works. So the first thing is to have a pattern that will be used with a string value, I will not be getting into regular expressions in detail here as I have all ready wrote a post on the basics or the RegExp class. So just create a new regular expression by one means or another by way of the RegExp constructor function called with the new keyword, or the literal syntax.
Once we have a pattern of what to look for then it is just a matter of calling the exec method off of the regular expression instance and passing a string to which we are going to look for the pattern. The result will be a result array, or the null value if an instance of the pattern is not found.
Anyway now that we have a basic example of exec out of the way we can get into something a little more involves that can be used as a way to get all instances of a pattern. The basic info that I would want is there with just one match, so now it is just a matter of getting the same set of info for all matches of a pattern in some given text.
So there is getting some basic info of the first match of a pattern in a string. However what if I want an array of objects for each instance of a pattern match in a string? The exec method can be used to make a method that can do just that, and in this section I will be going over a basic starting point for this kind of method. In addition I will also go over a few more examples of this kind of method that will start to be a little more refined.
So say I have a pattern with a global flag set, and a string that has two or more instances of that pattern. What I want is an array of objects that each have an index property for the starting index value for each instance of the pattern in the given string. The exec method can be used in conjunction with a loop such as a while loop to keep looping, calling exec each time until a null value is returned. Each time a pattern is found the result can be pushed to an array, building this array of results for each match.
So great that is the basic idea of what I would want, now it is just a matter of just making a few simple changes when it comes to the state of the Objects. There are certain additional values that I might want, such as the ending index of each pattern match. In this example it might not matter, but with other patterns that would be needed. In addition I might not want or even really need the original text for each object. Still this is a good start when it comes to making this kind of method.
I would just need to make a single simple change to the basic get index values method to get a result that is a little cleaner. The only change really that I think helps right away is to just push a new object with my own set of properties rather than just pushing the raw result of the exec method. The result is a cleaner, eaiser to follow set of plain old objects where I have a text property for the text of the match, and not just an index value but and end index value also. This data can then be used to help with any search and replace options later.