Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:RegExp:test

From MDC


Contents

[edit] Summary

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, NES3.0
ECMA Version: ECMA-262, Edition 3

[edit] Syntax

regexp.test([str])

[edit] Parameters

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

[edit] Description

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

[edit] Examples

[edit] 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);
}