String Search
JavaScript provides several methods to search within strings, check for the presence of substrings, or find patterns using regular expressions. Strings are immutable, so these methods do not change the original string but return new values like indices, booleans, or matched content.
Below is a structured overview of the key string search methods with practical examples.
indexOf()
Returns the index of the first occurrence of a substring. Returns -1
if the substring is not found.
let str = "Hello World";
console.log(str.indexOf("World")); // 6
console.log(str.indexOf("o")); // 4
console.log(str.indexOf("o", 5)); // 7
console.log(str.indexOf("x")); // -1
lastIndexOf()
Returns the index of the last occurrence of a substring. Returns -1
if the substring is not found.
let str = "Hello Hello";
console.log(str.lastIndexOf("Hello")); // 6
console.log(str.lastIndexOf("l")); // 8
console.log(str.lastIndexOf("l", 7)); // 3
console.log(str.lastIndexOf("x")); // -1
includes()
Checks whether a string contains a specified substring. Returns true
if found, otherwise false
.
let str = "Hello World";
console.log(str.includes("World")); // true
console.log(str.includes("world")); // false
console.log(str.includes("o", 5)); // true
console.log(str.includes("x")); // false
startsWith()
Checks if a string starts with a specified substring. Returns true
or false
.
let str = "Hello World";
console.log(str.startsWith("Hello")); // true
console.log(str.startsWith("World", 6)); // true
console.log(str.startsWith("hello")); // false
endsWith()
Checks if a string ends with a specified substring. Returns true
or false
.
let str = "Hello World";
console.log(str.endsWith("World")); // true
console.log(str.endsWith("Hello", 5)); // true
console.log(str.endsWith("world")); // false
search()
Searches a string using a regular expression and returns the index of the first match. Returns -1
if no match is found.
let str = "Hello World";
console.log(str.search(/World/)); // 6
console.log(str.search(/\\w+/)); // 0
console.log(str.search(/x/)); // -1
match()
Returns an array of matches for a regular expression, or null
if no match.
let str = "Hello World, Hello Universe";
console.log(str.match(/Hello/)); // ["Hello", index: 0, input: ..., groups: undefined]
console.log(str.match(/Hello/g)); // ["Hello", "Hello"]
console.log(str.match(/\\d/)); // null
matchAll()
Returns an iterator of all matches for a regular expression, including capturing groups. Requires the global flag /g
.
let str = "Hello World, Hello Universe";
let matches = [...str.matchAll(/Hello/g)];
console.log(matches);
// Output: Array of match objects: [{0: "Hello", index: 0, ...}, {0: "Hello", index: 13, ...}]
Quick Reference Table
Method | Description | Example |
---|---|---|
indexOf() | First occurrence index of substring | "Hello World".indexOf("World") // 6 |
lastIndexOf() | Last occurrence index of substring | "Hello Hello".lastIndexOf("Hello") // 6 |
includes() | Checks if substring exists | "Hello World".includes("World") // true |
startsWith() | Checks if string starts with substring | "Hello World".startsWith("World", 6) // true |
endsWith() | Checks if string ends with substring | "Hello World".endsWith("Hello", 5) // true |
search() | Finds first match using regex | "Hello World".search(/World/) // 6 |
match() | Returns array of regex matches | "Hello World, Hello Universe".match(/Hello/g) // ["Hello","Hello"] |
matchAll() | Returns iterator of all regex matches | [..."Hello World, Hello Universe".matchAll(/Hello/g)] |