본문 바로가기
JAVASCRIPT

메서드 (split)

by 코빈_ 2022. 8. 17.

split()

문자열에서 원하는 값을 추출하여 배열로 반환한다.

"문자열".split(구분자);
"문자열".split(정규식 표현);
"문자열".split(구분자, 갯수);

    const str1 = "javascript reference"
    const currentStr1 = str1.split('')         // ['j','a','v','a','s','c','r','i','p','t','','r','e','f','e','r','e','n','c','e']
    const currentStr2 = str1.split(' ')        // ['javascript', 'reference']
    const currentStr3 = str1.split('', 1)     // ['j']
    const currentStr4 = str1.split('', 2)     // ['j', 'a']
    const currentStr5 = str1.split(' ', 1)    // ['javascript']
    const currentStr6 = str1.split(' ', 2)    // ['javascript', 'reference']
    const currentStr7 = str1.split('j')        // ['','avascript reference']
    const currentStr8 = str1.split('a')       // ['j', 'v', 'script reference']
    const currentStr9 = str1.split('e')       // ['javascript r', 'f', 'r', 'nc', '']

    const str2 = "java/script refer/ence"
    const cuurrentStr10 = str2.split('/');   // ['java', 'script', 'refer', 'ence']

    const str3 = "java&script&refer!ence";
    const cuurrentStr11 = str3.split('!');           // ['java&script&refer', 'ence']
    const cuurrentStr12 = str3.split('&');          // ['java/script', 'refer!ence']
    const cuurrentStr13 = str3.split(/&|\!/);   // ['java', 'script', 'refer', 'ence']
    
    const str4 = "javascript reference";
    const cuurrentStr14 = str4.split('').join();                        // j,a,v,a,s,c,r,i,p,t, ,r,e,f,e,r,e,n,c,e
    const cuurrentStr15 = str4.split('').join('*');                    // j*a*v*a*s*c*r*i*p*t* *r*e*f*e*r*e*n*c*e
    const cuurrentStr16 = str4.split('').reverse().join('');       // ecnerefer tpircsavaj
    const cuurrentStr17 = str4.split('').reverse().join('^^');    // e^^c^^n^^e^^r^^e^^f^^e^^r^^ ^^t^^p^^i^^r^^c^^s^^a^^v^^a^^j

'JAVASCRIPT' 카테고리의 다른 글

메서드 (concat)  (2) 2022.08.17
메서드 (replace / replaceAll)  (2) 2022.08.17
메서드 (toUpperCase / toLowerCase)  (2) 2022.08.17
문자열 결합 / 템플릿 문자열  (2) 2022.08.17
정규식 표현  (4) 2022.08.16

댓글


INFORMATION

javascript

css

html