test

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

Syntax

regexp.test(str)

Parameters

regexp

The name of the regular expression. It can be a variable name or a literal.

str

(Optional) The string against which to match the regular expression. If omitted, the value of RegExp.input is used.

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

Example

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 ";
      Console.Write (str + midstring + re.source);
}