.split() 이란 문자열을 분할하는 메서드 입니다.
- 문법
- string.split( separator, limit )
- separator에는 분할의 기준을 넣는다. ex) 쉼표를 기준으로 분할 ' , '
- limit은 최대 분할개수를 정한다. 값을 정하지 않으면 전체를 다 분할
let data = 'red,yellow,green'
let jbSplit = data.split(',')
for(let idx in jbSplit){
console.log(jbSplit + '\n')
}
-----------------------------------------
red
yellow
green
let data = 'red,yellow_green'
let jbSplit = data.split(',|_')
for(let idx in jbSplit){
console.log(jbSplit + '\n')
}
-----------------------------------------
red
yellow
green
let data = 'red?,yellow,green'
let jbSplit = data.split('\\?')
for(let idx in jbSplit){
console.log(jbSplit + '\n')
}
-----------------------------------------
red
,yellow,green