search

Executes the search for a match between a regular expression and this String object.

Applies to

String

Syntax

search(regexp)

Parameters

regexp

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

Description

If successful, search returns the index of the match inside the string. Otherwise, it returns -1.

When you want to know whether a pattern is found in a string use search (similar to the regular expression test method); for more information (but slower execution) use match (similar to the regular expression exec method).

Example

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

function testinput(re, str){
      if (str.search(re) != -1)
            midstring = " contains ";
      else 
            midstring = " does not contain ";
      Console.Write (str + midstring + re.source);
}