JavaScript String Search
JavaScript methods for searching strings:
- String.indexOf()
- String.lastindexOf()
- String.startsWith()
- String.endsWithf()
String.indexOf()
The indexOf() method returns the index of (the position of)
the first occurrence of a specified text in a string:
Example
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate") // Returns 7
Try it Yourself »
JavaScript counts positions from zero.
0 is the first position in a
string, 1 is the second, 2 is the third ...
String.lastIndexOf()
The lastIndexOf() method returns the index of the last
occurrence of a specified text in a string:
Example
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate") // Returns 21
Try it Yourself »
Both indexOf(), and lastIndexOf() return -1
if the text is not found:
Example
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("John") // Returns -1
Try it Yourself »
Both methods accept a second parameter as the starting position for the search:
Example
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate", 15) // Returns 21
Try it Yourself »
The lastIndexOf() methods searches backwards
(from the end to the beginning), meaning:
if the second parameter is 15, the search starts at position
15, and searches to the beginning of the string.
Example
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate", 15) // Returns 7
Try it Yourself »
String.search()
The search() method searches a string for a specified value
and returns the position of the match:
Example
let str = "Please locate where 'locate' occurs!";
str.search("locate") // Returns 7
Try it Yourself »
Did You Notice?
The two methods, indexOf() and search(), are equal?
They accept the same arguments (parameters), and return the same value?
The two methods are NOT equal. These are the differences:
- The
search()method cannot take a second start position argument. - The
indexOf()method cannot take powerful search values (regular expressions).
You will learn more about regular expressions in a later chapter.
String.startsWith()
The startsWith() method returns true
if a string begins with a specified value, otherwise false:
Example
let text = "Hello world, welcome to the universe.";
text.startsWith("Hello") // Returns true
Try it Yourself »
Syntax
string.startsWith(searchvalue, start)
Parameter Values
| Parameter | Description |
|---|---|
| searchvalue | Required. The value to search for. |
| start | Optional. Default 0. The position to start the search. |
Examples
let text = "Hello world, welcome to the universe.";
text.startsWith("world") // Returns false
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 5) // Returns false
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 6) // Returns true
Try it Yourself »
Note: The startsWith() method is case sensitive.
The startsWith() method is not supported in Internet Explorer.
| Chrome 41 | Edge 12 | Firefox 17 | Safari 9 | Opera 28 |
String.endsWith()
The endsWith() method returns true
if a string ends with a specified value, otherwise false:
Example
Check if a string ends with "Doe":
var text = "John Doe";
text.endsWith("Doe") // Returns true
Try it Yourself »
Syntax
string.endswith(searchvalue, length)
Parameter Values
| Parameter | Description |
|---|---|
| searchvalue | Required. The value to search for. |
| length | Optional. The length to search. |
Check in the 11 first characters of a string ends with "world":
let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11) // Returns true
Note: The endsWith() method is case sensitive.
The endsWith() method is not supported in Internet Explorer.
| Chrome 41 | Edge 12 | Firefox 17 | Safari 9 | Opera 36 |
