O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Checking to See If a String Contains a Substring

var s1 = "I think therefore I am"; var a = s1.indexOf("think");     // a = 2 var b = s1.indexOf("thought"); // b = -1

The simplest way to check to see if one string is a substring of another is to use .indexOf(substring) on the main string. This function returns an index of the location of the first occurrence, or –1 if the substring is not found.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required