Back to JavaScript Notes
Topic #298

String Search

You will be able to search JavaScript strings for a substring, test whether text exists, and extract matching parts using indexOf, includes, and match.

What it is

String search means asking a string whether it contains another string, where that substring starts, or what parts match a pattern. JavaScript provides simple methods for literal text and regular expressions for flexible patterns. Related terms include substring, index, regular expression, capture group, and global flag.

indexOf returns the first position of a literal substring or -1 if absent. includes returns true or false. match returns an array of matches or null when a regular expression finds nothing.

Why it matters

  • Validate user input, such as checking whether an email contains @.
  • Find positions for slicing, replacing, or highlighting text.
  • Extract structured data, such as dates, IDs, or numbers, from free-form text.
  • Write readable existence checks without comparing to -1.
  • Use regular expressions only when literal search is not enough.

Syntax or steps

Start with the smallest useful pattern: choose the method based on the answer you need.

const text = "hello world";

text.indexOf("world"); // 6
text.includes("world"); // true
text.match(/world/); // ["world"]

"a,b,c".includes("b"); // true

For indexOf, you can pass a starting position to search later in the string. For match, add the g flag to find all matches instead of only the first.

Example

const log = "user: 42, action: login, time: 2024-05-17";

const hasLogin = log.includes("login");
const firstComma = log.indexOf(",");
const numbers = log.match(/\d+/g);
const dateParts = log.match(/(\d{4})-(\d{2})-(\d{2})/);

console.log(hasLogin); // true
console.log(firstComma); // 8
console.log(numbers); // ["42", "2024", "05", "17"]
console.log(dateParts[0]); // "2024-05-17"
console.log(dateParts[1]); // "2024"

Part by part: includes checks for the literal word login. indexOf finds the first comma position. match(/\d+/g) returns every run of digits because \d means digit, + means one or more, and g means global. match(/(\d{4})-(\d{2})-(\d{2})/) returns the full date plus captured groups, so dateParts[1] is the year.

Common mistakes

  • Using if (text.indexOf("x")). This fails when the match is at index 0 because 0 is falsy. Use text.indexOf("x") !== -1 or text.includes("x").
  • Forgetting case sensitivity. "Hello".includes("hello") is false. Normalize with toLowerCase() when appropriate.
  • Expecting match to return all matches without g. Use /pattern/g for all matches.
  • Assuming match returns an empty array when nothing matches. It returns null, so check before using array methods.

When to use it

Use indexOf when you need a position, includes when you only need yes or no, and match when you need to extract text with a pattern.

MethodReturnsBest forWatch out
indexOfNumber or -1First literal positionDo not treat result as boolean
includesBooleanSimple existence checksLiteral only, no regex
matchArray or nullExtracting substringsNeeds regex and null check

Practice

  1. Guided: Given "user@example.com", use indexOf to find @, includes to check for example.com, and match(/([^@]+)@(.+)/) to extract the username and domain. Expected: index 4, boolean true, and username/domain values "user" and "example.com".
  2. Challenge: Find all numbers in "order 12, qty 3, total 45". Hint: use match(/\d+/g).

Quick check

Question: Why is if ("abc".indexOf("a")) a bug?

Answer: indexOf("a") returns 0, and 0 is falsy, so the condition fails even though the match exists. Use !== -1 or includes.

Summary

String search in JavaScript is about choosing the right return type: position, boolean, or extracted matches. indexOf and includes are best for literal text, while match is best when you need regular expressions to pull out structured pieces.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

String Search – FAQs

Quick answers about learning String Search in JavaScript.

This free note from Coding Now Tech Institute explains String Search in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including String Search, is 100% free with no signup required.
With focused practice, most students grasp String Search in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now