JavaScript exec()Method
JavaScript RegExp Object
Definition and Usage
The exec() method is used to retrieve the match of a regular expression in a string.
If there is a matching value in the string, it returns the matched value; otherwise, it returns null.
Syntax
RegExpObject.exec(string)
| Parameters | Description |
|---|---|
| string | Required. The string to be searched |
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the exec() method.
Examples
Examples
Globally search for the "Hello" and "EXAMPLE" strings in a string:
var str="Hello world!";
//Find "Hello"
var patt=/Hello/g;
var result=patt.exec(str);
document.write("Return value:" + result);
//Find "EXAMPLE"
patt=/EXAMPLE/g;
result=patt.exec(str);
document.write("<br>Return value:" + result);
Output of the above example:
Returned value: Hello
Returned value: null
Returned value: null
Try it yourself »
JavaScript RegExp Object Other Extensions