JavaScript tutorial:
exec method

 

Applies To: Regular Expression Object

The exec method executes a search for a match in a specified string.

Syntax

rgexp.exec(str)

The exec method syntax has these parts:

Part

Description

rgexp

Required. A Regular Expression object. Can be a variable name or a literal.

str

Required. The string to perform a search on.

Return value

Returns an array if there is a match or null if there is no match.

Remarks

The results of an exec method search are placed into an array.

If the exec method does not find a match, it returns null. If it finds one or more matches, the exec method returns an array, and the RegExp object is updated to reflect the results of the search.

The following example illustrates the use of the exec method:

function ExecDemo()
{
    var s = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp";
    var r = new RegExp("g", "i");
    var a = r.exec(s);
    document.write(a);
    r.compile("g");
    var a = r.exec(s);
    document.write(a);
}
document.write(ExecDemo());

To run the code, paste it into JavaScript Editor, and click the Execute button.

See Also:RegExp Object, Regular Expression Object Methods, Regular Expression Object Properties, Regular Expression Syntax