JavaScript String Search Methods
JavaScript provides several methods that search strings: indexOf
, lastIndexOf
, search
, and the regular expression method test
. These basic methods tell you whether a match has been found. The first three return the location in the string of the match, or -1
if no match is found. The test
method returns true
or false
.
Recent versions of JavaScript have added new methods for searching strings: includes
, startsWith
, and endsWith
. These also return true
or false
to indicate whether a match was found.
The string match
method and the regular expression exec
method provide more details about search results. These are discussed on another page.
indexOf
To search a string using the indexOf
method, you invoke it on the string you are searching, and pass the substring you are searching for. The indexOf
method returns an integer indicating the location of the first found instance of the substring in the string, or -1
if it is not found. An example demonstrates:
var str = 'A string to test JavaScript string methods';
var pos = str.indexOf('string'); // 2
You can pass an optional second argument to indexOf
to specify the index location at which to begin the search:[1]
var str = 'A string to test JavaScript string methods';
// start search at index location 10
var pos = str.indexOf('string', 10); // 28
lastIndexOf
While the indexOf
method searches from left to right, the lastIndexOf
method searches from right to left, from the end of the string toward the beginning. So it will return the starting location of the last instance of the substring we are searching for, as we demonstrate here:
var str = 'A string to test JavaScript string methods';
var pos = str.lastIndexOf('string'); // 28
Like the indexOf
method, the lastIndexOf
method supports an optional second argument to specify the starting location of the search.[2] It specifies the number of characters from the end of the string to exclude from the search:
var str = 'A string to test JavaScript string methods';
// start searching backwards 20 characters from end
var pos = str.lastIndexOf('string', 20); // 2
The indexOf
and lastIndexOf
methods perform case-sensitive searches. Case-insensitive searches can be performed with the string search
method and with the regular expression test
method.
JavaScript's String search Method
JavaScript's string search
method accepts a single argument: a pattern to search for in the string it is invoked on. This argument can be a regular expression, or a string which will automatically be converted to a regular expression. The search
method is similar to the indexOf
and lastIndexOf
methods in that it returns the location of the first found match, or -1
if it is not found. We demonstrate with a case-insensitive search using the same string as shown in examples above:
var str = 'A string to test JavaScript string methods';
var re = /String/i; // i flag for case-insensitive
var pos = str.search(re); // 2
Check Return Value of Search
The following demonstrates how to check the return value of indexOf
, lastIndexOf
, or search
for a found substring:
var str = 'A string to test JavaScript string methods';
var pos = str.indexOf('A '); // 0
if ( pos !== -1 ) {
console.log('Found at location ' + pos);
}
The return value should be compared with -1
since the substring you are searching for could occur at the beginning of the string, and in that case would return 0
and convert to false
.[3]
Regular Expression test Method
The regular expression test
method can be used to perform the same search as was shown above using the string search
method. To do so we invoke the test
method on the regular expression object, and pass the string as an argument:
var str = 'A string to test JavaScript string methods';
var re = /String/i;
console.log( re.test(str) ); // true
The test
method returns true
if the pattern is found in the string, false
if it is not.
String includes Method
Pass a string to the includes
method and it will return true
or false
to indicate whether that string was found in the string the method was invoked on:
var str = 'A string to test JavaScript string methods';
console.log( str.includes('JavaScript') ); // true
An optional second argument can be used to specify the location at which to start the search.
startsWith and ensWith Methods
The startsWith
and endsWith
methods each accept a single argument: a string to search for at the beginning of the string for startsWith
, or at the end of the string for endsWith
:
var str = 'A string to test JavaScript string methods';
console.log( str.startsWith('A') ); // true
console.log( str.endsWith('!') ); // false
The startsWith
and endsWith
methods return true
or false
to indicate whether the substring was found.
Browser Support for Newer Methods
At the time of this writing, approximately 95% of browsers support these newer string search methods. Details can be found at caniuse.com.
- If the second argument to
indexOf
is greater than or equal to string length, -1 is returned. If the second argument is negative, the entire string is searched. ^ - If the second argument to
lastIndexOf
is greater than the string length minus 1, the entire string is searched. If the value is zero or negative, the method returns -1. ^ - Find out more at Data Type and Type Conversion in JavaScript. ^