---
id: javascript-string-search
title: "JavaScript String Search"
category: "Frontend"
status: "draft"
verification_status: "conceptual"
canonical_id: ""
aliases: ["string search", "indexOf", "search", "match", "includes", "startsWith", "endsWith"]
duplicate_of: ""
source_trust_level: "B"
confidence_score: 0.90
created_at: 2026-06-23
updated_at: 2026-06-23
review_reason: ""
merge_history: []
tags: ["javascript", "js", "web", "frontend", "w3schools", "strings", "search", "regexp"]
raw_sources: ["https://www.w3schools.com/js/js_string_search.asp"]
applied_in: []
github_commit: ""
---

# [[JavaScript String Search]]

## 🎯 한 줄 통찰 (One-line insight)
JavaScript offers eight string-search methods — `indexOf`/`lastIndexOf` return positions, `search`/`match`/`matchAll` accept regular expressions, and `includes`/`startsWith`/`endsWith` return booleans. [S1]

## 🧠 핵심 개념 (Core concepts)
- **`indexOf()`** — returns the index of the first occurrence of a string, or -1 if not found; positions are counted from zero. [S1]
- **`lastIndexOf()`** — returns the index of the last occurrence; like `indexOf()` it returns -1 if not found. [S1]
- **Second parameter = start position** — both `indexOf()` and `lastIndexOf()` accept a second parameter as the starting position; `lastIndexOf()` searches backwards from that position. [S1]
- **`search()`** — searches for a string (or regular expression) and returns the position of the match. [S1]
- **`indexOf()` vs `search()` are NOT equal** — `search()` cannot take a second start-position argument; `indexOf()` cannot take regular expressions. [S1]
- **`match()`** — returns an array of matches against a string or regex; without the `g` modifier it returns only the first match. [S1]
- **`matchAll()`** — returns an iterator of matches (ES2020); a regex parameter must set the global flag `g` or a TypeError is thrown. [S1]
- **`includes()` / `startsWith()` / `endsWith()`** — return `true`/`false`; all are case sensitive ES6 features. [S1]

## 🧩 추출된 패턴 (Extracted patterns)
- **Find-or-handle-missing** — call `indexOf()` and compare to -1 to branch on presence. [S1]
- **Search from an offset** — pass a start position to skip an earlier portion of the string. [S1]
- **Regex matching with flags** — use `/pattern/g` for all matches and `/pattern/gi` for case-insensitive global matching. [S1]
- **Boolean membership check** — prefer `includes()` / `startsWith()` / `endsWith()` when you only need a yes/no answer. [S1]

## 📖 세부 내용 (Details)
**String Search Methods**
The page lists eight methods: `indexOf()`, `lastIndexOf()`, `search()`, `match()`, `matchAll()`, `includes()`, `startsWith()`, and `endsWith()`. [S1]

**indexOf()** — returns the index (position) of the first occurrence of a string in a string, or -1 if not found. JavaScript counts positions from zero. [S1]
```javascript
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate");
```
With a second parameter as the starting position: [S1]
```javascript
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate", 15);
```

**lastIndexOf()** — returns the index of the last occurrence of a specified text. Both `indexOf()` and `lastIndexOf()` return -1 if the text is not found. [S1]
```javascript
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("locate");
```
Returns -1 when not found: [S1]
```javascript
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("John");
```
`lastIndexOf()` searches backwards (from end to beginning); if the second parameter is `15`, the search starts at position 15 and searches toward the beginning. [S1]
```javascript
let text = "Please locate where 'locate' occurs!";
text.lastIndexOf("locate", 15);
```

**search()** — searches a string for a string (or a regular expression) and returns the position of the match. [S1]
```javascript
let text = "Please locate where 'locate' occurs!";
text.search("locate");
```
```javascript
let text = "Please locate where 'locate' occurs!";
text.search(/locate/);
```

**Did You Notice?** — `indexOf()` and `search()` are NOT equal. The differences: `search()` cannot take a second start-position argument; `indexOf()` cannot take powerful search values (regular expressions). [S1]

**match()** — returns an array containing the results of matching a string against a string (or regular expression). If a regular expression does not include the `g` modifier (global search), `match()` returns only the first match. [S1]
```javascript
let text = "The rain in SPAIN stays mainly in the plain";
text.match("ain");
```
```javascript
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/);
```
```javascript
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);
```
```javascript
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);
```

**matchAll()** — returns an iterator containing the results of matching against a string or regular expression. If the parameter is a regular expression, the global flag (`g`) must be set, otherwise a TypeError is thrown; for case-insensitive search the insensitive flag (`i`) must be set. `matchAll()` is an ES2020 feature and does not work in Internet Explorer. [S1]
```javascript
const iterator = text.matchAll("Cats");
```
```javascript
const iterator = text.matchAll(/Cats/g);
```
```javascript
const iterator = text.matchAll(/Cats/gi);
```

**includes()** — returns `true` if a string contains a specified value, otherwise `false`. It is case sensitive and an ES6 feature. [S1]
```javascript
let text = "Hello world, welcome to the universe.";
text.includes("world");
```
```javascript
let text = "Hello world, welcome to the universe.";
text.includes("world", 12);
```

**startsWith()** — returns `true` if a string begins with a specified value, otherwise `false`; case sensitive ES6 feature. A start position can be specified. [S1]
```javascript
let text = "Hello world, welcome to the universe.";
text.startsWith("Hello");
```
```javascript
let text = "Hello world, welcome to the universe.";
text.startsWith("world")
```
```javascript
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 5)
```
```javascript
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 6)
```

**endsWith()** — returns `true` if a string ends with a specified value, otherwise `false`; case sensitive ES6 feature. [S1]
```javascript
let text = "John Doe";
text.endsWith("Doe");
```
```javascript
let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11);
```

## 🛠️ 적용 사례 (Applied in summary)
The page's own snippets are the canonical applied examples — locating `"locate"` with `indexOf`/`lastIndexOf`/`search`, matching `"ain"` with regex flags, and boolean checks against `"Hello world, welcome to the universe."`. No external project/commit applications found in the source.

## 💻 코드 패턴 (Code patterns)
Find first position (or -1):
```javascript
let index = text.indexOf("locate");
```
Global, case-insensitive regex match:
```javascript
text.match(/ain/gi);
```
Boolean membership check:
```javascript
text.includes("world");
text.startsWith("Hello");
text.endsWith("Doe");
```

## ⚖️ 비교 및 선택 기준 (Comparison & decision criteria)
- **`indexOf()` vs `search()`** — use `indexOf()` when you need a start-position argument; use `search()` when you need regular-expression power. They accept different arguments and are not interchangeable. [S1]
- **`indexOf()` vs `includes()`** — use `indexOf()` when you need the position; use `includes()` when a boolean presence check is enough. [S1]
- **`match()` vs `matchAll()`** — `match()` without `g` returns only the first match; `matchAll()` returns an iterator over all matches but requires the `g` flag for regex. [S1]

## ⚖️ 모순 및 업데이트 (Contradictions & updates)
`matchAll()` does not work in Internet Explorer (ES2020). No contradictions found in the source.

## ✅ 검증 상태 및 신뢰도
- **상태:** draft
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
- **신뢰 점수:** 0.90
- **중복 검사 결과:** 신규 생성 (New discovery)

## 🔗 지식 그래프 (Knowledge Graph)
- **상위/루트:** [[JavaScript Tutorial]]
- **관련 개념:** [[JavaScript Strings]], [[JavaScript String Methods]], [[JavaScript String Templates]], [[JavaScript RegExp]]
- **참조 맥락:** Referenced whenever locating, matching, or testing for substrings within text.

## 📚 출처 (Sources)
- [S1] W3Schools — JavaScript String Search — https://www.w3schools.com/js/js_string_search.asp

## 📝 변경 이력 (Change history)
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript String Search" page (Astra wiki-curation, P-Reinforce v3.1 format).
