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 (3)
ai generated ?
Partially AI-assisted for formating, with human input for context and refinement.
yeah