Index of the Last Match

Discover what you can do with the 'lastIndex' property of a RegExp object with the help of an example.

We'll cover the following

lastIndex property

Depending on the method you use, matches aren’t performed globally, but, instead one at a time on every execution of the method. If you’re working like this, then you might want to consider taking advantage of the lastIndex property of the object. This property provides the index of where to start for the next match. In other words, it contains the position of the last match as well as the length of the match.

With this property, we can potentially capture sections of the string between one match and the following one. By doing this, you can capture said sections of the original string.

Get hands-on with 1200+ tech skills courses.