Executes the search for a match between a regular expression and a specified string. Returns true or false.

Method of RegExp
Implemented in JavaScript 1.2
ECMAScript Edition ECMAScript 3rd Edition




The name of the regular expression. It can be a variable name or a literal.
The string against which to match the regular expression.


When you want to know whether a pattern is found in a string use the test method (similar to the String.search method); for more information (but slower execution) use the exec method (similar to the String.match method). As with exec (or in combination with it), test called multiple times on the same global regular expression instance will advance past the previous match.

Gecko 8.0 note
(Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5)

Prior to Gecko 8.0 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5), test() was implemented incorrectly; when it was called with no parameters, it would match against the value of the previous input (RegExp.input property) instead of against the string "undefined". This is fixed; now /undefined/.test() correctly results in true, instead of an error.


Example: Using test

The following example prints a message which depends on the success of the test:

function testinput(re, str){
   if (re.test(str)) {
      midstring = " contains ";
   } else {
      midstring = " does not contain ";
   document.write (str + midstring + re.source);

Tags (1)

Contributors to this page: evilpie, Sheppy, franksvalli, Brettz9, Dynamis, Mgjbot, niels_bom, Maian, Dria
Last updated by: niels_bom,