The exec Method

Learn about the exec method, which is called on RegExp objects by studying the example provided in this lesson.

Using the exec method

The exec method executes a search in a specified string to the full extent of the expression. This means that if there are capturing groups, they’ll also be taken into account.

Just like with our previous example, this method makes use of the internal state as long as you add the global or sticky flags. So, on every subsequent call to the method, the lastIndex property will be updated, and a new match will be returned. If these flags aren’t used, then, no matter how many times you call the method, the same first match will always be returned.

Get hands-on with 1200+ tech skills courses.