DEV Community

Cover image for Master JavaScript String Search: The Ultimate Guide for Developers
Raj Aryan
Raj Aryan

Posted on

Master JavaScript String Search: The Ultimate Guide for Developers

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
Enter fullscreen mode Exit fullscreen mode


`

βœ… 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() or search()
  • Need matches? Use match() or matchAll()
  • Just checking? Go for includes(), startsWith(), or endsWith()

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)

Collapse
 
sumim_hasan_hcr profile image
Sumim Hasan

ai generated ?

Collapse
 
er-raj-aryan profile image
Raj Aryan

Partially AI-assisted for formating, with human input for context and refinement.

Collapse
 
sumim_hasan_hcr profile image
Sumim Hasan

yeah