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

Description

Returns true or false for a match between a regular expression and a string.

  • Will advance past the previous match when called multiple times on the same regular expression.

Syntax

Signature Description
result = regexp.exec(string)Returns true or false for a match between a regular expression and a 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 test() method on a string.


var aRegExp = /and/g;
var aString = new String 'one and two and three and four';
var testValues = new Array();
var i = 0;

while (aRegExp.test(aString)==true) {
  testValues[i] = 'test() found a result.' + aRegExp.test(aString)
	+ ' Match will continue at index: ' 
	+ aRegExp.lastIndex + '\n'; 
  i++;
}

alert(testValues);

Press the button below to action the above code:

Related Tutorials

JavaScript Intermediate Tutorials - Lesson 9 - Regular Expressions