Learn to Find, Match, and Extract text in JavaScript like a pro.
JavaScript offers multiple string search methodsโbut which one should you use and when?
In this post, Iโll break down 8 must-know methods with code examples, differences, and use cases.
๐ Quick Overview: String Search Methods in JavaScript
| Method | Use Case | Returns |
|---|---|---|
indexOf() |
First index of a substring | Number (-1 if not found) |
lastIndexOf() |
Last index of a substring | Number (-1 if not found) |
search() |
Match string or RegExp | Number (-1 if not found) |
match() |
Find pattern matches | Array (or null) |
matchAll() |
All matches with RegExp iterator | Iterator |
includes() |
Checks if string contains substring | Boolean |
startsWith() |
Checks if string starts with a value | Boolean |
endsWith() |
Checks if string ends with a value | Boolean |
๐ 1. indexOf() โ Find the First Occurrence
let text = "Please locate where 'locate' occurs!";
console.log(text.indexOf("locate")); // 7
`
โ
Returns the index of the first occurrence.
๐ซ Returns -1 if not found.
๐ 2. lastIndexOf() โ Find the Last Occurrence
js
let text = "Please locate where 'locate' occurs!";
console.log(text.lastIndexOf("locate")); // 21
Useful when strings repeat and you want the last position.
๐งช 3. search() โ Search with RegExp
js
let text = "Please locate where 'locate' occurs!";
console.log(text.search(/locate/)); // 7
๐ก Similar to indexOf() but supports RegExp.
โ Cannot take a second argument for position.
๐งฉ 4. match() โ Return Matches as an Array
js
let text = "The rain in SPAIN stays mainly in the plain";
console.log(text.match(/ain/g)); // ['ain', 'ain', 'ain']
โ
Best for pattern extraction.
๐ Without g flag, returns only the first match.
๐ 5. matchAll() โ Powerful Iterative Matching
js
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);
๐ Introduced in ES2020
โ ๏ธ Not supported in Internet Explorer
๐ 6. includes() โ Check if String Contains a Substring
js
let text = "Hello world, welcome to the universe.";
console.log(text.includes("world")); // true
โ
Clean, readable, and ideal for boolean checks.
โ ๏ธ Case-sensitive and doesnโt support RegExp.
๐ 7. startsWith() โ Does the String Start With?
js
let text = "Hello world";
console.log(text.startsWith("Hello")); // true
๐ Perfect for validating prefixes like "https" or "Mr.".
๐งท 8. endsWith() โ Does the String End With?
js
let text = "John Doe";
console.log(text.endsWith("Doe")); // true
๐ Ideal for file extensions, like ".jpg" or ".pdf".
๐ก Pro Tip: indexOf() vs search()
| Feature | indexOf() |
search() |
|---|---|---|
| Accepts RegExp? | โ No | โ Yes |
| Start Position? | โ Yes | โ No |
Use search() for patterns, and indexOf() for direct substrings.
๐ง Final Thoughts
JavaScript gives you surgical precision with string searches.
- Want the position? Use
indexOf()orsearch() - Need matches? Use
match()ormatchAll() - Just checking? Go for
includes(),startsWith(), orendsWith()
Once you master these, youโll write cleaner, faster, and more powerful JavaScript.
๐ Recommended Books to Level Up Your Skills
1๏ธโฃ JavaScript: The Definitive Guide (7th Edition) by David Flanagan
๐ฅ The ultimate JavaScript bible trusted by developers for over two decades.
Whether you're a beginner or advanced, this book gives you a deep, modern understanding of JavaScript with Promises, classes, modules, and more.
๐ Perfect for: Web developers, full-stack devs, and JavaScript enthusiasts.
2๏ธโฃ Learning React (2nd Edition) by Alex Banks & Eve Porcello
โ๏ธ Master modern React patterns with real-world examples.
Covers everything from Hooks to advanced component patterns. A must-read if you work with React or plan to.
๐ Perfect for: Frontend developers, React beginners, and anyone building SPAs.
๐ฌ Letโs Talk
Have a cool use case for matchAll() or a regex trick that saved your project?
Drop it in the comments ๐
If you liked this post, follow me @er-raj-aryan for more JavaScript and React tutorials!`
Top comments (4)
ai generated ?
Partially AI-assisted for formating, with human input for context and refinement.
yeah
awesomeness ...I also wanted to learn how to compare search strings and see if a string or array includes certain letters in one string