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 index0because0is falsy. Usetext.indexOf("x") !== -1ortext.includes("x"). - Forgetting case sensitivity.
"Hello".includes("hello")isfalse. Normalize withtoLowerCase()when appropriate. - Expecting
matchto return all matches withoutg. Use/pattern/gfor all matches. - Assuming
matchreturns an empty array when nothing matches. It returnsnull, 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.
| Method | Returns | Best for | Watch out |
|---|---|---|---|
indexOf | Number or -1 | First literal position | Do not treat result as boolean |
includes | Boolean | Simple existence checks | Literal only, no regex |
match | Array or null | Extracting substrings | Needs regex and null check |
Practice
- Guided: Given
"user@example.com", useindexOfto find@,includesto check forexample.com, andmatch(/([^@]+)@(.+)/)to extract the username and domain. Expected: index4, booleantrue, and username/domain values"user"and"example.com". - Challenge: Find all numbers in
"order 12, qty 3, total 45". Hint: usematch(/\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.