In JavaScript, there are several methods available to search for a substring or pattern within a string. These methods help you locate specific text, check for the presence of text, or retrieve matches based on patterns.
JavaScript String Search Methods
indexOf()
: Finds the first occurrence of a value.lastIndexOf()
: Finds the last occurrence of a value.includes()
: Checks if a string contains a value.search()
: Searches for a match and returns the position.match()
: Returns an array of matches against a regular expression.matchAll()
: Returns an iterator of all matches.startsWith()
: Checks if a string starts with specified characters.endsWith()
: Checks if a string ends with specified characters.
1. indexOf()
The indexOf()
method returns the index of the first occurrence of a specified value in a string. If the value is not found, it returns -1
.
let text = 'Hello, World!'; let position = text.indexOf('World'); console.log(position); // Output: 7
Key Points:
- It is case-sensitive.
- Returns
-1
if the value is not found.
2. lastIndexOf()
The lastIndexOf()
method returns the index of the last occurrence of a specified value in a string. It searches the string backward.
let text = 'Hello, World! World!'; let position = text.lastIndexOf('World'); console.log(position); // Output: 14
Key Points:
- It is case-sensitive.
- Returns
-1
if the value is not found.
3. includes()
The includes()
method checks if a string contains a specified value. It returns true
or false
.
let text = 'Hello, World!'; let result = text.includes('World'); console.log(result); // Output: true
Key Points:
- It is case-sensitive.
- Returns a boolean value.
4. search()
The search()
method searches a string for a specified value or regular expression and returns the position of the first match.
let text = 'Hello, World!'; let position = text.search('World'); console.log(position); // Output: 7
Key Points:
- You can use a regular expression for more complex searches.
- Returns
-1
if the value is not found.
5. match()
The match()
method searches a string for a match against a regular expression and returns the matches as an array.
let text = 'The rain in Spain stays mainly in the plain'; let matches = text.match(/ain/g); console.log(matches); // Output: ['ain', 'ain', 'ain']
Key Points:
- Useful for extracting matched patterns.
- Returns
null
if no matches are found.
6. matchAll()
The matchAll()
method returns an iterator of all results matching a string against a regular expression, including capturing groups.
let text = 'The rain in Spain stays mainly in the plain'; let matches = text.matchAll(/ain/g); for (let match of matches) { console.log(match); }
Key Points:
- Provides detailed information about matches, including groups.
- Returns an iterator.
7. startsWith()
The startsWith()
method checks if a string starts with specified characters and returns true
or false
.
let text = 'Hello, World!'; let result = text.startsWith('Hello'); console.log(result); // Output: true
8. endsWith()
The endsWith()
method checks if a string ends with specified characters and returns true
or false
.
let text = 'Hello, World!'; let result = text.endsWith('World!'); console.log(result); // Output: true
Summary
These methods are essential for searching and manipulating strings in JavaScript, providing flexibility and efficiency in handling text-based operations.