What is JavaScript String Search ?
In JavaScript, strings are one of the most commonly used data types and being able to search within them is vital for tasks like validating user inputs, extracting data and building dynamic applications. JavaScript offers multiple methods to search for substrings or patterns within a string efficiently.
Why Learn String Search Methods?
String search methods in JavaScript help:
- Locate specific text within a string.
- Validate or filter content.
- Extract matching patterns using regular expressions.
By mastering these methods, you can handle text-based operations with precision.
Common String Search Methods in JavaScript
Here are the primary string search methods, along with detailed explanations and examples.
1. indexOf()
The indexOf() method returns the first occurrence of a specified value in a string. If the value is not found, it returns -1.
Syntax:
string.indexOf(searchValue, startPosition);
Example:
let text = "Hello, JavaScript!";
console.log(text.indexOf("JavaScript")); // Output: 7
console.log(text.indexOf("Python")); // Output: -1
Explanation:
- In the first example, “JavaScript” is found at index 7.
- In the second example, “Python” is not present, so it returns -1.
2. lastIndexOf()
The lastIndexOf() method searches the string from the end and returns the last occurrence of a specified value.
Syntax:
string.lastIndexOf(searchValue, startPosition);
Example:
let phrase = "JavaScript is fun. JavaScript is versatile.";
console.log(phrase.lastIndexOf("JavaScript")); // Output: 16
Explanation:
The last occurrence of “JavaScript” is at index 16.
3. search()
The search() method looks for a match between a regular expression and the string. It returns the index of the first match or -1 if no match is found.
Syntax:
string.search(regexp);
Example:
let sentence = "Learning JavaScript is fun!";
console.log(sentence.search(/JavaScript/)); // Output: 9
console.log(sentence.search(/Python/)); // Output: -1
Explanation:
- The search() method is ideal for pattern-based searches.
- Here, /JavaScript/ is a regular expression used to find “JavaScript”.
4. match()
The match() method retrieves the results of a string matching against a regular expression.
Syntax:
string.match(regexp);
Example:
let data = "The rain in Spain stays mainly in the plain.";
console.log(data.match(/ain/g)); // Output: ["ain", "ain", "ain"]
Explanation:
- The /ain/g regular expression searches for all occurrences of “ain” globally (g flag).
5. includes()
The includes() method checks if a string contains a specified value. It returns true or false.
Syntax:
string.includes(searchValue, startPosition);
Example:
let message = "Hello, world!";
console.log(message.includes("world")); // Output: true
console.log(message.includes("Python")); // Output: false
Explanation:
- includes() is straightforward for checking substring existence.
6. startsWith()
The startsWith() method checks if a string begins with a specified value.
Syntax:
string.startsWith(searchValue, startPosition);
Example:
let sentence = "JavaScript is awesome!";
console.log(sentence.startsWith("Java")); // Output: true
console.log(sentence.startsWith("Script")); // Output: false
7. endsWith()
The endsWith() method checks if a string ends with a specified value.
Syntax:
string.endsWith(searchValue, length);
Example:
let tagline = "Coding is fun!";
console.log(tagline.endsWith("fun!")); // Output: true
console.log(tagline.endsWith("is")); // Output: false
Practical Use Cases for String Search Methods
Validating User Input
function validateEmail(email) {
return email.includes("@") && email.endsWith(".com");
}
console.log(validateEmail("example@gmail.com")); // Output: true
console.log(validateEmail("example.com")); // Output: false
Searching Keywords in Text
let article = "JavaScript is versatile and powerful.";
console.log(article.indexOf("versatile")); // Output: 13
console.log(article.includes("Python")); // Output: false
Extracting Data with Patterns
let log = "Error at line 42: unexpected token";
let match = log.match(/line \d+/);
console.log(match[0]); // Output: line 42