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