String.contains Redirect 1

This is an experimental technology, part of the Harmony (ECMAScript 6) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

The contains() method 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 MooTools compatibility issues for details.

Syntax

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.";

console.log(str.contains("To be"));       // true
console.log(str.contains("question"));    // true
console.log(str.contains("nonexistent")); // false
console.log(str.contains("To be", 1));    // false
console.log(str.contains("TO BE"));       // false

Polyfill

This method has been added to the ECMAScript 6 specification and may not be available in all JavaScript implementations yet. However, you can easily polyfill this method:

if ( !String.prototype.contains ) {
    String.prototype.contains = function() {
        return String.prototype.indexOf.apply( this, arguments ) !== -1;
    };
}

Specifications

Specification Status Comment
ECMAScript Language Specification 6th Edition (ECMA-262) Draft Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support Not supported 18 (18) Not supported Not supported Not supported
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support Not supported Not supported 18.0 (18) Not supported Not supported Not supported

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() implementation to break. As a result, this change was disabled in Firefox 17. String.prototype.contains is available one version later, Firefox 18.

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). MooTools 1.5+ changed the signature to match the ES6 standard.

See also

Document Tags and Contributors

Last updated by: Sheppy,
Hide Sidebar