str.split(separator, limit?)
separator:字符串或正則表達式limit:可選,限制返回數組最大長度'How are you doing today?'.split(' ');
// ["How","are","you","doing","today?"]
'Hello'.split('');
// ["H","e","l","l","o"]
'Hello'.split('', 3);
// ["H","e","l"]
'2:3:4:5'.split(':');
// ["2","3","4","5"]
'|a|b|c'.split('|');
// ["", "a", "b", "c"] // 首位分隔符產生空串
// 多種空白字符分割單詞
'hello world\ttest\nnew'.split(/\s+/);
// ["hello","world","test","new"]
// 保留分隔符(捕獲組)
'a,b;c'.split(/([,;])/);
// ["a", ",", "b", ";", "c"]
// 零寬斷言分割(不消耗字符)
'JavaScript'.split(/(?=[A-Z])/);
// ["Java", "Script"]
// 駝峰轉下劃線
'camelCaseString'.split(/(?=[A-Z])/).join('-').toLowerCase();
// "camel-case-string"
// 反轉單詞順序
'the sky is blue'.split(' ').reverse().join(' ');
// "blue is sky the"
''.split(',') → [""](長度 1),非空數組split 會忽略 g,每次匹配重置 lastIndexString.prototype.matchAll 或手寫迭代器'😀😁'.split('') → ["\uD83D","\uDE00","\uD83D","\uDE01"] 破壞 Emoji,用 [...'😀😁'] 或 Array.fromfunction parseCSV(line) {
// 簡易:不處理引號內逗號、換行
return line.split(',').map(s => s.trim());
}
parseCSV('a, b ,c'); // ["a","b","c"]
生產環境請用成熟庫(papaparse、csv-parse)處理引號、轉義、多行。