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 (4)

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

Collapse
 
overflow profile image
overFlow

awesomeness ...I also wanted to learn how to compare search strings and see if a string or array includes certain letters in one string