Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:String:search

From MDC


Contents

[edit] Summary

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

Method of String
Implemented in: JavaScript 1.2
ECMA Version: ECMA-262, Edition 3

[edit] Syntax

search(regexp)

[edit] Parameters

regexp 
A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

[edit] Description

If successful, search returns the index of the regular expression 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).

[edit] Examples

[edit] Example: Using search

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