JavaScript exec()Method

RegExp 对象参考手册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

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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

Try it yourself »


RegExp 对象参考手册JavaScript RegExp Object Other Extensions