JavaScript & jQuery Tutorials

JS Home Right Arrow JS Reference Right Arrow RegExp Right Arrow exec()

exec()  RegExp  instance method


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.


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


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


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);


Press the button below to action the above code:

Related Tutorials

JavaScript Intermediate Tutorials - Lesson 9 - Regular Expressions


go to home page Homepage go to top of page Top