str.split([separator[, limit]]) tách chuỗi thành array theo separator.
Ví dụ cơ bản
let a = "hello,hi,bonjour,namaste";
let greetings = a.split(',');
console.log(greetings); // ['hello', 'hi', 'bonjour', 'namaste']
Separator bị loại bỏ khỏi kết quả.
Separator rỗng: tách thành từng ký tự
let a = "hello";
console.log(a.split("")); // ['h', 'e', 'l', 'l', 'o']
Không truyền separator: trả về array 1 phần tử (chuỗi gốc)
let a = "hello world"; console.log(a.split()); // ["hello world"]
Tham số limit: giới hạn số phần tử
"a,b,c,d".split(",", 2); // ['a', 'b']
Regex separator
"a1b2c3".split(/\d/); // ['a', 'b', 'c', '']