자바스크립트 문자열 split()
예
단어 나누기:
let text = "How are you doing today?";
const myArray = text.split(" ");
단어를 나누고 두 번째 단어를 반환합니다.
let text = "How are you doing today?";
const myArray = text.split(" ");
let word = myArray[1];
공백을 포함한 문자 분할:
const myArray = text.split("");
제한 매개변수를 사용합니다.
const myArray = text.split(" ", 3);
아래에 더 많은 예가 있습니다.
정의 및 사용
이 split()
메서드는 문자열을 하위 문자열 배열로 분할합니다.
이 split()
메서드는 새 배열을 반환합니다.
이 split()
메서드는 원래 문자열을 변경하지 않습니다.
(" ")가 구분 기호로 사용되는 경우 문자열은 단어 간에 분할됩니다.
통사론
string.split(separator, limit)
매개변수
Parameter | Description |
separator | Optional. A string or regular expression to use for splitting. If omitted, an array with the original string is returned. |
limit | Optional. An integer that limits the number of splits. Items after the limit are excluded. |
반환 값
유형 | 설명 |
정렬 | 분할된 값을 포함하는 배열입니다. |
더 많은 예
문자열을 문자로 분할하고 두 번째 문자를 반환합니다.
const myArray = text.split("");
문자를 구분 기호로 사용:
const myArray = text.split("o");
구분자 매개변수가 생략되면 원래 문자열이 있는 배열이 반환됩니다.
const myArray = text.split();
브라우저 지원
split()
ECMAScript1(ES1) 기능입니다.
ES1(JavaScript 1997)은 모든 브라우저에서 완벽하게 지원됩니다.
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |