JavaScript String Search
String Search Methods
JavaScript String indexOf()
TheindexOf() method returns theindex (position)of thefirst occurrence of a string in a string, or it returns -1 if the string is not found:
Example
let index = text.indexOf("locate");
Note
JavaScript counts positions from zero.
0 is the first position in a string, 1 is the second, 2 is the third, ...
JavaScript String lastIndexOf()
ThelastIndexOf() method returns theindex of thelast occurrence of a specified text in a string:
Example
let index = text.lastIndexOf("locate");
BothindexOf(), andlastIndexOf() return -1if the text is not found:
Example
let index = text.lastIndexOf("John");
Both methods accept a second parameter as the starting position for the search:
Example
let index = text.indexOf("locate", 15);
ThelastIndexOf() methods searches backwards (from the end to the beginning), meaning:if the second parameter is15, the search starts at position 15, and searches to the beginning of the string.
Example
text.lastIndexOf("locate", 15);
JavaScript String search()
Thesearch() method searches a string for a string (or a regular expression)and returns the position of the match:
Examples
text.search("locate");
text.search(/locate/);
Did You Notice?
The two methods,indexOf() andsearch(), areequal?
They accept the same arguments (parameters), and return the same value?
The two methods areNOT 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.
JavaScript String match()
Thematch() method returns an array containing the results of matchinga string against a string (or a regular expression).
Examples
Perform a search for "ain":
text.match("ain");
Perform a search for "ain":
text.match(/ain/);
Perform a global search for "ain":
text.match(/ain/g);
Perform a global, case-insensitive search for "ain":
text.match(/ain/gi);
Note
If a regular expression does not include theg modifier (global search),match() will return only the first match in the string.
Read more about regular expressions in the chapterJS RegExp.
JavaScript String matchAll()
ThematchAll() method returns an iterator containing the results of matchinga string against a string (or a regular expression).
If the parameter is a regular expression, the global flag (g) must be set, otherwisea TypeError is thrown.
If you want to search case insensitive, the insensitive flag (i) must be set:
JavaScript String includes()
Theincludes() method returns true if a string contains a specified value.
Otherwise it returnsfalse.
Examples
Check if a string includes "world":
text.includes("world");
Check if a string includes "world". Start at position 12:
text.includes("world", 12);
JavaScript String startsWith()
ThestartsWith() method returnstrueif a string begins with a specified value.
Otherwise it returnsfalse:
Examples
Returns true:
text.startsWith("Hello");
Returns false:
text.startsWith("world")
A start position for the search can be specified:
Returns false:
text.startsWith("world", 5)
Returns true:
text.startsWith("world", 6)
JavaScript String endsWith()
TheendsWith() method returnstrueif a string ends with a specified value.
Otherwise it returnsfalse:
Examples
Check if a string ends with "Doe":
text.endsWith("Doe");
Check if the 11 first characters of a string ends with "world":
text.endsWith("world", 11);
Complete JavaScript Reference
For a complete reference to all JavaScript properties and methods, with full descriptions and many examples, go to:
W3Schools' Full JavaScript Reference.
The reference inludes all JavaScript updates from 1999 to 2025.

