Files
2nd/10_Wiki/Topic_JavaScript/JavaScript_String_Search.md
T
koriweb 9609c04755 docs(10_Wiki): W3Schools 위키화 — HTML/CSS/JavaScript(core)
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더).
- Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외)
- Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체)
- Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속)
각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 19:21:18 +09:00

8.9 KiB

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

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 equalsearch() 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]

let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate");

With a second parameter as the starting position: [S1]

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]

let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("locate");

Returns -1 when not found: [S1]

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]

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]

let text = "Please locate where 'locate' occurs!";
text.search("locate");
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]

let text = "The rain in SPAIN stays mainly in the plain";
text.match("ain");
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/);
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);
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]

const iterator = text.matchAll("Cats");
const iterator = text.matchAll(/Cats/g);
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]

let text = "Hello world, welcome to the universe.";
text.includes("world");
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]

let text = "Hello world, welcome to the universe.";
text.startsWith("Hello");
let text = "Hello world, welcome to the universe.";
text.startsWith("world")
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 5)
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]

let text = "John Doe";
text.endsWith("Doe");
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):

let index = text.indexOf("locate");

Global, case-insensitive regex match:

text.match(/ain/gi);

Boolean membership check:

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)

📚 출처 (Sources)

📝 변경 이력 (Change history)

  • 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript String Search" page (Astra wiki-curation, P-Reinforce v3.1 format).