Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript exec() Method

JavaScript exec() Method

RegExp Object Reference JavaScript RegExp Object


Definition and Usage

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

If a match is found 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


Example

Example

Search globally for the strings "Hello" and "RUNOOB" in a string:

var

str

=

"

Hello world!

"

;

//

Find "Hello"

var

patt

=/

Hello

/

g

;

var

result

=

patt

.

exec

(

str

)

;

document

.

write

(

"

Return value:

"

result

)

;

//

Find "RUNOOB"

patt

=/

RUNOOB

/

g

;

result

=

patt

.

exec

(

str

)

;

document

.

write

(

"


Return value:

"

result

)

;

The output of the above example is:

Returned value: Hello

Returned value: null

Try it »


RegExp Object Reference JavaScript RegExp Object