Easy Tutorial
❮ Event Onunload Prop Style Clip ❯

JavaScript exec() Method


Definition and Usage

The exec() method is used to search for a match in a string using a regular expression.

It returns the matched value if found in the string, otherwise it returns null.

Syntax

Parameter Description
string Required. The string to be searched

Browser Support

All major browsers support the exec() method.


Example

Search for "Hello" and "tutorialpro" globally in a string:

var str = "Hello world!";
// Search for "Hello"
var patt = /Hello/g;
var result = patt.exec(str);
document.write("Return value: " + result);
// Search for "tutorialpro"
patt = /tutorialpro/g;
result = patt.exec(str);
document.write("<br>Return value: " + result);

The above example outputs:


❮ Event Onunload Prop Style Clip ❯