Example
Search a string for the character "e":
var str = "The best things in life are free";
var patt = new RegExp("e");
var res = patt.exec(str);
Since there is an "e" in the string, the result of res will be:
e
Try it Yourself »
Definition and Usage
The exec() method tests for a match in a string.
This method returns the matched text if it finds a match, otherwise it returns null.
Browser Support
Method | |||||
---|---|---|---|---|---|
exec() | Yes | Yes | Yes | Yes | Yes |
Syntax
RegExpObject.exec(string)
Parameter Values
Parameter | Description |
---|---|
string | Required. The string to be searched |
Return Value
Type | Description |
---|---|
Array | An array containing the matched text if it finds a match, otherwise it returns null |
Technical Details
JavaScript Version: | 1.2 |
---|
More Examples
Example
Do a global search, and test for "Hello" and "w3ii" in a string:
// The string:
var str = "Hello world!";
// Look for "Hello"
var patt = /Hello/g;
var result = patt.exec(str);
// Look for
"w3ii"
var patt2 = /w3ii/g;
result2 = patt2.exec(str);
The output of the code above will be:
Hello // match for "Hello"
null
// no match for "w3ii"
Try it Yourself »
< JavaScript RegExp Object