String.prototype.contains()

  • Revision slug: JavaScript/Reference/Global_Objects/String/contains
  • Revision title: contains
  • Revision id: 347263
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment

Revision Content

{{ js_minversion_header("1.8.6") }}

Determines whether one string may be found within another string, returning true or false as appropriate.

Note: Web sites using MooTools 1.2 should consider upgrading to a more recent version. See {{anch("MooTools compatibility issues")}} for details.

Method of Function
Implemented in JavaScript 1.8.6
ECMAScript Edition None (Harmony Proposal)

Syntax

var contained = str.contains(searchString [, position]);

Parameters

searchString
A string to be searched for within this string.
position
The position in this string at which to begin searching for searchString; defaults to 0.

Description

This method lets you determine whether or not a string contains another string.Examples

var str = "To be, or not to be, that is the question.";

print(str.contains("To be"));    // true
print(str.contains("question")); // true
print(str.contains("To be", 1)); // false

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatNo() }} {{ CompatGeckoDesktop("19") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatNo() }} {{ CompatGeckoDesktop("19") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

Notes

MooTools compatibility issues

It's been reported that some Web sites using MooTools 1.2 broke on Firefox 17. This version of MooTools checks whether String.prototype.contains() exists and, if it doesn't, adds its own. With the introduction of this function in Firefox 17, the behavior of that check changed in a way that causes code based on MooTools' String.prototype.contains() implmentation to break. As a result, this change was backed out of Firefox 17.

MooTools 1.3+ forces its own version of String.prototype.contains(), so Web sites relying on it should not break. However, you should note that MooTools 1.3+ signature and ECMAScript 6 signatures for this method differ (on the second argument). The MooTools team is aware of this situation and as considering changing its signature.

Revision Source

<div>
  {{ js_minversion_header("1.8.6") }}</div>
<p>Determines whether one string may be found within another string, returning <code>true</code> or <code>false</code> as appropriate.</p>
<div class="note">
  <p><strong>Note:</strong> Web sites using MooTools 1.2 should consider upgrading to a more recent version. See {{anch("MooTools compatibility issues")}} for details.</p>
</div>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2" scope="row">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function" title="JavaScript/Reference/Global_Objects/Function">Function</a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.8.6</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>None (Harmony Proposal)</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code class="brush:js;">var <em>contained</em> = <em>str</em>.contains(<em>searchString</em> [, <em>position</em>])</code>;</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>searchString</code></dt>
  <dd>
    A string to be searched for within this string.</dd>
  <dt>
    <code>position</code></dt>
  <dd>
    The position in this string at which to begin searching for <code><var>searchString</var></code>; defaults to 0.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p>This method lets you determine whether or not a string contains another string.Examples</p>
<pre class="brush:js;">
var str = "To be, or not to be, that is the question.";

print(str.contains("To be"));    // true
print(str.contains("question")); // true
print(str.contains("To be", 1)); // false
</pre>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatGeckoDesktop("19") }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatGeckoDesktop("19") }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="Notes">Notes</h2>
<h3 id="MooTools_compatibility_issues">MooTools compatibility issues</h3>
<p>It's been <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=789036">reported</a> that some Web sites using MooTools 1.2 broke on Firefox 17. This version of MooTools checks whether <code>String.prototype.contains()</code> exists and, if it doesn't, adds its own. With the introduction of this function in Firefox 17, the behavior of that check changed in a way that causes code based on MooTools' <code>String.prototype.contains()</code> implmentation to break. As a result, this change was <a href="https://hg.mozilla.org/releases/mozilla-aurora/rev/086db97198a8" title="https://bugzilla.mozilla.org/show_bug.cgi?id=793781">backed out</a> of Firefox 17.</p>
<p>MooTools 1.3+ forces its own version of <code>String.prototype.contains()</code>, so Web sites relying on it should not break. However, you should note that <a href="http://mootools.net/docs/core/Types/String#String:contains">MooTools 1.3+ signature</a> and ECMAScript 6 signatures for this method differ (on the second argument). <a href="https://github.com/mootools/mootools-core/issues/2402">The MooTools team is aware of this situation and as considering changing its signature.</a></p>
Revert to this revision