--- 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).