exec()  RegExp  instance method JS Home  <<  JS Reference  <<  RegExp  <<  exec()

Description

Returns a result array, or null from an executed search in a specified string.

  • If the match fails, the exec() method returns null.
  • The returned array contains the matched text and then one element for each capturing parenthesis containing the text that was matched.
  • Will advance past the previous match when called multiple times on the same regular expression.
  • For efficiency use the RegExp.test() method if you are only interested in finding out whether a match exists.

Syntax

Signature Description
result = regexp.exec(string)Returns a result array, or null from an executed search in a specified string.

Parameters

Signature Description
regexpThe regular expression.
  • This can be literal format or a variable name.
stringThe string to match the regular expression with.

Examples

The code below displays the exec() method on some string.


var aRegExp  = /(e{2,}(r))(e)/i;
var aString  = 'were only here for the beeeRe';
var bString  = 'oranges and lemons';
var execValues = new Array(2);

execValues[0] = 'Match found? ' + aRegExp.exec(aString) + '\n';
execValues[1] = 'Match found? ' + aRegExp.exec(bString);

alert(execValues);

Press the button below to action the above code:

Related Tutorials

JavaScript Intermediate Tutorials - Lesson 9 - Regular Expressions

JavaScript Basics

JavaScript Basics

JavaScript Intermediate

JavaScript Intermediate

JavaScript Advanced

JavaScript Advanced

JavaScript Reference

JavaScript Entities

Globals

RegExp

Class Properties
prototype
Instance Properties
constructor
global
ignoreCase
lastIndex
multiline
source
Class Methods
None.
Instance Methods Getters
exec()
test()
toString()
Instance Methods Setters
None.

Statements

Operators