자바스크립트 문자열 indexOf()
예
"환영" 문자열 검색:
let text = "Hello world, welcome to the universe.";
let result = text.indexOf("welcome");
"Welcome" 문자열 검색:
let text = "Hello world, welcome to the universe.";
let result = text.indexOf("Welcome");
"e"의 첫 번째 항목 찾기:
let text = "Hello world, welcome to the universe.";
text.indexOf("e");
위치 5에서 시작하는 "e"의 첫 번째 항목을 찾습니다.
let text = "Hello world, welcome to the universe.";
text.indexOf("e", 5);
"a"의 첫 번째 항목 찾기:
let text = "Hello world, welcome to the universe.";
text.indexOf("a");
정의 및 사용
이 indexOf()
메서드는 문자열에서 값이 처음 나타나는 위치를 반환합니다.
값을 찾을 수 없는 경우 메서드 는 indexOf()
-1을 반환합니다.
메서드 는 indexOf()
대소문자를 구분합니다.
통사론
string.indexOf(searchvalue, start)
매개변수
Parameter | Description |
searchvalue | Required. The string to search for. |
start | Optional. The position to start from (default is 0). |
반환 값
유형 | 설명 |
번호 | 검색 값이 발생하는 첫 번째 위치입니다. 발생하지 않으면 -1입니다. |
문자열 indexOf()와 문자열 search() 의 차이점
메서드 는 indexOf()
정규식을 검색할 수 없습니다.
search()
시작 위치 인수를 사용할 수 없습니다 .
브라우저 지원
indexOf()
ECMAScript1(ES1) 기능입니다.
ES1(JavaScript 1997)은 모든 브라우저에서 완벽하게 지원됩니다.
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |