Sub topic 2
Finding Text Inside a String
This chapter assumes you know basic JavaScript syntax, array methods such as map, and how to run code in Node.js or a browser console. It also assumes you know that string methods return new strings and never change the original.
Searching
The search methods answer two kinds of question. indexOf and lastIndexOf return a position, or -1 if there is no match. includes, startsWith, and endsWith return a boolean. All of them are case-sensitive.
| Method | Returns | Common mistake |
|---|---|---|
| `indexOf(s, from)` | first index or `-1` | writing `if (str.indexOf(x))`, which treats index 0 as false |
| `lastIndexOf(s)` | last index or `-1` | forgetting that it searches backward |
| `includes(s)` | boolean | passing a regex, which throws a TypeError |
Extracting with slice and substring
Both slice and substring take a start index and an optional end index, and the end is exclusive. They behave differently with negative arguments. slice counts a negative index back from the end of the string. substring treats any negative value as 0.
const path = "/docs/guide/strings.md";
console.log(path.includes("guide")); // true
console.log(path.startsWith("/docs")); // true
console.log(path.endsWith(".md")); // true
console.log(path.indexOf("/", 1)); // 5
const file = path.slice(path.lastIndexOf("/") + 1);
console.log(file); // strings.md
console.log("hello".slice(-3)); // llo
console.log("hello".substring(-3)); // hello (negative treated as 0)Use slice unless you have a specific reason not to.
Transforming, splitting, and joining
toUpperCase, toLowerCase, trim, trimStart, and trimEnd clean up text. padStart, padEnd, and repeat produce fixed-width output. split turns a string into an array, and join turns an array back into a string.
const csv = " alice, bob ,carol ";
const names = csv.split(",").map((n) => n.trim());
console.log(names); // ["alice", "bob", "carol"]
console.log(names.join(" | ")); // alice | bob | carol
console.log("7".padStart(3, "0")); // 007
console.log("ab".repeat(3)); // abababReplacing
const text = "cat and cat";
console.log(text.replace("cat", "dog")); // dog and cat
console.log(text.replaceAll("cat", "dog")); // dog and dog
console.log(text.replace(/cat/g, "dog")); // dog and dog
const date = "2026-11-25".replace(/(\d+)-(\d+)-(\d+)/, "$3/$2/$1");
console.log(date); // 25/11/2026
const prices = "apple 3, pear 5".replace(/\d+/g, (n) => String(n * 2));
console.log(prices); // apple 6, pear 10In the replacement string, $1, $2, and so on refer to capture groups. A replacer function receives each match and returns the text to put in its place.
Exercise
What does "a-b-c".replace("-", "+") return, and how would you change every dash?
Show answer
It returns "a+b-c", because a string pattern replaces only the first match. Use "a-b-c".replaceAll("-", "+") or "a-b-c".replace(/-/g, "+") to get "a+b+c".
Exercise
Why does "x.txt".substring(-4) differ from "x.txt".slice(-4)?
Show answer
substring treats -4 as 0 and returns "x.txt". slice counts back from the end and returns ".txt".