mozilla

Revision 63760 of String.prototype.search()

  • Revision slug: JavaScript/Reference/Global_Objects/String/search
  • Revision title: search
  • Revision id: 63760
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment 47 words added, 55 words removed

Revision Content

Summary

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

Method of String
Implemented in JavaScript 1.2
ECMAScript Edition ECMAScript 3rd Edition

Syntax

string.search(regexp)

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

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

{{ gecko_callout_heading("8.0") }}

Prior to Gecko 8.0 {{ geckoRelease("8.0") }}, search() was implemented incorrectly; when it was called with no parameters or with undefined, it would match against the string "undefined", instead of matching against the empty string. This is fixed; now "a".search() and "a".search(undefined) correctly return 0.

Examples

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

{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/String/search", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/search", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/search" } ) }}

Revision Source

<h3 name="Summary">Summary</h3>
<p>Executes the search for a match between a regular expression and this <code>String</code> object.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/String" title="en/JavaScript/Reference/Global_Objects/String"><code>String</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript 1.2</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 3rd Edition</td> </tr> </tbody>
</table>
<h3 name="Syntax">Syntax</h3>
<p><code><em>string</em>.search(<em>regexp</em>)</code></p>
<h3 name="Parameters">Parameters</h3>
<dl> <dt><code>regexp</code> </dt> <dd>A <a href="/en/JavaScript/Reference/Global_Objects/RegExp" title="en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp"> regular expression</a> object. If a non-RegExp object <code>obj</code> is passed, it is implicitly converted to a RegExp by using <code>new RegExp(obj)</code>.</dd>
</dl>
<h3 name="Description">Description</h3>
<p>If successful, search returns the index of the regular expression inside the string. Otherwise, it returns -1.</p>
<p>When you want to know whether a pattern is found in a string use <code>search</code> (similar to the regular expression <code><a href="/en/JavaScript/Reference/Global_Objects/RegExp/test" title="en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp/test">test</a></code> method); for more information (but slower execution) use <code><a href="/en/JavaScript/Reference/Global_Objects/String/match" title="en/Core_JavaScript_1.5_Reference/Global_Objects/String/match">match</a></code> (similar to the regular expression <code><a href="/en/JavaScript/Reference/Global_Objects/RegExp/exec" title="en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp/exec">exec</a></code> method).</p>
<div class="geckoVersionNote">
<p>{{ gecko_callout_heading("8.0") }}</p>
<p>Prior to Gecko 8.0 {{ geckoRelease("8.0") }}, <code>search()</code> was implemented incorrectly; when it was called with no parameters or with <code>undefined</code>, it would match against the string "undefined", instead of matching against the empty string. This is fixed; now <code>"a".search()</code> and <code>"a".search(undefined)</code> correctly return 0.</p>
</div>
<h3 name="Examples">Examples</h3>
<h4 name="Example:_Using_search">Example: Using <code>search</code></h4>
<p>The following example prints a message which depends on the success of the test.</p>
<pre class="brush: js">function testinput(re, str){
   if (str.search(re) != -1)
      midstring = " contains ";
   else
      midstring = " does not contain ";
   document.write (str + midstring + re);
}
</pre>
<p>{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/String/search", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/search", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/search" } ) }}</p>
Revert to this revision