자바스크립트 문자열 검색
자바스크립트 검색 방법
- 문자열 indexOf()
- 문자열 lastIndexOf()
- 문자열 시작()
- 문자열 끝()
자바스크립트 문자열 indexOf()
이 indexOf()
메서드는 first
문자열에서 지정된 텍스트의 발생 인덱스(위치)를 반환합니다.
예시
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate");
메모
JavaScript는 0부터 위치를 계산합니다.
0은 문자열의 첫 번째 위치, 1은 두 번째, 2는 세 번째, ...
자바스크립트 문자열 lastIndexOf()
이 메서드는
문자열에서 지정된 텍스트가 마지막으로lastIndexOf()
나타나는 인덱스를 반환합니다 .
예시
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate");
둘 다 indexOf()
, 그리고 lastIndexOf()
텍스트가 없으면 -1을 반환합니다.
예시
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("John");
두 방법 모두 검색의 시작 위치로 두 번째 매개변수를 허용합니다.
예시
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate", 15);
메서드 는 lastIndexOf()
역방향으로(끝에서 시작으로) 검색합니다. 즉, 두 번째 매개변수가 15
이면 검색은 위치 15에서 시작하여 문자열의 시작 부분으로 검색합니다.
예시
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate", 15);
자바스크립트 문자열 검색()
이 search()
메서드는 문자열에서 지정된 값을 검색하고 일치하는 위치를 반환합니다.
예시
let str = "Please locate where 'locate' occurs!";
str.search("locate");
눈치채셨나요?
두 가지 방법 indexOf()
및 search()
는 동일합니까?
그들은 동일한 인수(매개변수)를 허용하고 동일한 값을 반환합니까?
두 가지 방법은 같지 않습니다 . 차이점은 다음과 같습니다.
- 메서드는 두
search()
번째 시작 위치 인수를 사용할 수 없습니다. - 이
indexOf()
메서드는 강력한 검색 값(정규 표현식)을 사용할 수 없습니다.
정규 표현식에 대한 자세한 내용은 이후 장에서 배우게 될 것입니다.
JavaScript 문자열 일치()
match() 메서드는 문자열에서 정규식과 일치하는 항목을 검색하고 일치 항목을 Array 객체로 반환합니다.
실시예 1
"ain"에 대한 문자열 검색:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);
JS RegExp 장에서 정규식에 대해 자세히 읽어보십시오 .
메모
정규식에 g 수정자가 포함되어 있지 않으면( 전역 검색을 수행하기 위해) match() 메서드는 문자열에서 첫 번째 일치 항목만 반환합니다.
통사론
string.match(regexp)
regexp | Required. The value to search for, as a regular expression. |
Returns: | An Array, containing the matches, one item for each match, or null if no match is found |
실시예 2
"ain"에 대해 대소문자를 구분하지 않는 전역 검색을 수행합니다.
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);
JavaScript 문자열은()를 포함합니다
includes()
문자열에 지정된 값이 포함되어 있으면 메서드가 true를 반환합니다 .
예시
let text = "Hello world, welcome to the universe.";
text.includes("world");
통사론
string.includes(searchvalue, start)
searchvalue | Required. The string to search for |
start | Optional. Default 0. Position to start the search |
Returns: | Returns true if the string contains the value, otherwise false |
JS Version: | ES6 (2015) |
문자열에 "world"가 포함되어 있는지 확인하고 위치 12에서 검색을 시작합니다.
let text = "Hello world, welcome to the universe.";
text.includes("world", 12);
브라우저 지원
includes()
ES6 기능(JavaScript 2015)입니다.
모든 최신 브라우저에서 지원됩니다.
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
includes()
Internet Explorer에서는 지원되지 않습니다.
자바스크립트 문자열 startsWith()
이 startsWith()
메서드는 true
문자열이 지정된 값으로 시작하면 반환하고, 그렇지 않으면 false
다음 을 반환합니다.
예시
let text = "Hello world, welcome to the universe.";
text.startsWith("Hello");
통사론
string.startsWith(searchvalue, start)
매개변수 값
Parameter | Description |
---|---|
searchvalue | Required. The value to search for. |
start | Optional. Default 0. The position to start the search. |
예
let text = "Hello world, welcome to the universe.";
text.startsWith("world") // Returns false
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 5) // Returns false
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 6) // Returns true
메모
메서드 는 startsWith()
대소문자를 구분합니다.
브라우저 지원
startsWith()
ES6 기능(JavaScript 2015)입니다.
모든 최신 브라우저에서 지원됩니다.
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
startsWith()
Internet Explorer에서는 지원되지 않습니다.
JavaScript 문자열 끝()
이 endsWith()
메서드는 true
문자열이 지정된 값으로 끝나면 반환하고, 그렇지 않으면 false
다음 을 반환합니다.
예시
문자열이 "Doe"로 끝나는지 확인:
var text = "John Doe";
text.endsWith("Doe");
통사론
string.endswith(searchvalue, length)
매개변수 값
Parameter | Description |
---|---|
searchvalue | Required. The value to search for. |
length | Optional. The length to search. |
"world"로 끝나는 문자열의 첫 11개 문자를 확인하십시오.
let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11);
메모
메서드 는 endsWith()
대소문자를 구분합니다.
브라우저 지원
endsWith()
ES6 기능(JavaScript 2015)입니다.
모든 최신 브라우저에서 지원됩니다.
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
endsWith()
Internet Explorer에서는 지원되지 않습니다.
완전한 문자열 참조
전체 문자열 참조를 보려면 다음으로 이동하십시오.
참조에는 모든 문자열 속성 및 메서드에 대한 설명과 예가 포함되어 있습니다.