exec()  RegExp  instance method getterS2C Home  « Globals « 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 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

go to home page Homepage go to top of page Top