Mozilla.com

test

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

Syntax

regexp.test([str])

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.

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

Examples

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

Page last modified 18:06, 28 Feb 2007 by Mgjbot

Tags:

Files (0)