JS: String.prototype.split
str.split(separatorStr)-
Split the string by separator (a string).
Result is Array. The separator is not included in result unless using regex arg with capture.
const xx = "ab cd".split(" "); console.log(JSON.stringify(xx) === `["ab","cd"]`); str.split(regex)-
Split the string by regex. 〔see JS: Regular Expression Syntax〕
If regex contains capturing parentheses, the captured parts are included in result.
// split string by regex const xx = "a b c"; console.log(xx.split(/ +/)); // [ "a", "b", "c" ] // split string by regex with capture, to include the separator in result const xx = "a-b-c"; console.log(xx.split(/(-)/)); // [ "a", "-", "b", "-", "c" ] str.split(sep, n)-
get just the first n items.
const xx = "a,b,c,d"; console.log(xx.split(",", 2)); // [ "a", "b" ] str.split()-
Return array of 1 element, the element is the whole string.
const xx = "a,b,c"; console.log(xx.split()); // [ "a,b,c" ] str.split("")-
Return array, each element is the code unit.
🛑 WARNING: string methods do not work the way you think if it contains characters outside of Unicode Basic Multilingual Plane (e.g. emoji 🦋.). See JS: String Code Unit
console.log("abc".split("")); // [ "a", "b", "c" ] console.log("a🦋c".split("")); // [ "a", "�", "�", "c" ]
String Separator Example
Repeated space in string may result empty string element in array.
/* Repeated space in string may result empty string element in array. */ console.log( JSON.stringify(" a b c ".split(" ")) === `["","a","","b","c",""]`, ); /* using regex still have problem at the edges */ console.log( JSON.stringify(" a b c ".split(/ +/)) === `["","a","b","c",""]`, ); /* solution is use trim first, then regex */ console.log( JSON.stringify(" a b c ".trim().split(/ +/)) === `["a","b","c"]`, );
Use filter to remove empty string in array.
〔see Array.prototype.filter〕
Other Example
Split by a character that doesn't exist, return full string
// split by a char that doesn't exist, returns full string console.log(JSON.stringify("abc".split("-")) === `["abc"]`);
JS String.prototype
- JS: String.prototype.constructor
- JS: String.prototype.length
- JS: String.prototype.at (Extract Char at Index)
- JS: String.fromCharCode (Char ID to Char) ❌
- JS: String.prototype.concat
- JS: String.prototype.repeat
- JS: String.prototype.trim
- JS: String.prototype.trimStart
- JS: String.prototype.trimEnd
- JS: String.prototype.padStart
- JS: String.prototype.padEnd
- JS: String.prototype.slice
- JS: String.prototype.substring ❌
- JS: String.prototype.substr
- JS: String.prototype.indexOf
- JS: String.prototype.lastIndexOf
- JS: String.prototype.includes
- JS: String.prototype.startsWith
- JS: String.prototype.endsWith
- JS: String.prototype.search
- JS: String.prototype.match
- JS: String.prototype.matchAll
- JS: String.prototype.replace
- JS: String.prototype.split
- JS: String.prototype.toLowerCase
- JS: String.prototype.charAt (Extract Char at Index) ❌
- JS: String.prototype.charCodeAt (Char to Char ID) ❌
- JS: String.prototype.codePointAt (Char to Char ID) ❌