JavaScript RegExp exec() Method

Definition and usage

exec() Method tests the matching items in the string.

This is a general matching pattern method.

If a match is found, the result array is returned, otherwise null.

Example

Example 1

Search for the character "e" in the string:

let text = "The best things in life are free";
let result = /e/.exec(text);

Try it yourself

Example 2

Global search "Hello" and "W3School" in the string:

let text = "Hello world!";
// Find "Hello"
let result1 = /Hello/.exec(text);
// Find "W3School"
let result2 = /W3School/.exec(text);

Try it yourself

Syntax

regexp.exec(string)

Parameter

Parameter Description
string Required. The string to be searched.

Return value

Type Description
Array If a match is found, the array contains the matching text, otherwise return null.

Technical details

Return value

Array, stores the matching results. If no match is found, the value is nullThe following describes the format of the returned array.

is thrown.

Type Description
TypeError An exception is thrown if the object called this method is not RegExp.

Description

in all RegExp pattern matching methods, String pattern matching methods includeexec() is the most powerful. It is a general method that is easier to use than RegExp.test(),String.replace() and String.match() is very complex.

exec() will search the string string, we get the text that matches the regular expression regexp text that matches. If exec() When a match is found, it will return an array of results. Otherwise, return nullThe 0th element of the returned array is the text that matches the expression. The 1st element is the text that matches regexp the first subexpression matches the text (if it exists). The second element is the text that matches the regexp text that matches the second subexpression, and so on. Usually, the length property of the array declares the number of elements in the array. In addition to the array elements and length property,exec() also returns two properties. The index property declares the position of the first character of the matching text. The input property refers to string. In calling a non-global RegExp object's exec() method, the returned array is the same as the method called String.match() returns the same method.

is called for a non-global pattern exec() method, it will search and return the above results. However, when regexp is a global regular expression whenexec() behaves a bit more complex. It regexp attribute lastIndex to start searching the string from the character specified by string to the position of the first character of the matching text. This means that you can regexp the lastIndex property of exec() method to traverse all matching text in the string. When exec() can no longer find matching text, it will return null, and reset the lastIndex property to 0. If you need to start searching a new string after completing a pattern match in another string, you must manually reset the lastIndex property to 0.

Note that no matter regexp whether it is a global pattern,exec() will add complete details to the array it returns. This is exec() and String.match() differences, the latter returns much less information in the global mode. In fact, calling exec() The method is the only one that can obtain complete pattern matching information of the global pattern.

Browser support

exec() Is ECMAScript1 (ES1) feature.

All browsers fully support ES1 (JavaScript 1997):

Chrome IE Edge Firefox Safari Opera
Supports Supports Supports Supports Supports Supports

Regular expression search methods

In JavaScript, regular expression text search can be completed using different methods.

UsagePattern (pattern)As regular expressions, these are the most commonly used methods:

Example Description
text.match(pattern) String method match()
text.search(pattern) String method search()
pattern.exec(text) RexExp method exec()
pattern.test(text) RexExp method test()