String.prototype.split()
Added in ves6 · Updated March 13, 2026 · String Methods
javascript es6 string
The split() method divides a string into an ordered list of substrings, puts these substrings into an array, and returns the array. The separation is done by searching for a pattern that is either a string or a regular expression. This method has been part of ECMAScript since ES6 (ES2015).
Syntax
split(separator)
split(separator, limit)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
separator | string | RegExp | undefined | — | The pattern describing where each split should occur. Can be a string, regular expression, or omitted. Omitting the separator returns the entire string as a single element. |
limit | number | undefined | A non-negative integer specifying a limit on the number of substrings to include in the array. |
Return Value
Returns an array of strings split at each point where the separator occurs. If the separator is a regex with capturing groups, the captured groups are also included in the result.
Examples
Basic usage with a string separator
const message = "hello world javascript";
const words = message.split(" ");
console.log(words);
// ['hello', 'world', 'javascript']
// Splitting on empty string returns each character
const letters = "abc".split("");
console.log(letters);
// ['a', 'b', 'c']
// Omitting separator returns entire string as one element
const whole = "hello".split();
console.log(whole);
// ['hello']
Using a regular expression separator
const text = "one,two,three,four";
const items = text.split(/,/);
console.log(items);
// ['one', 'two', 'three', 'four']
// Splitting on multiple characters
const numbers = "1a2b3c4d".split(/[abc]/);
console.log(numbers);
// ['1', '2', '3', '4d']
Limiting the number of splits
const csv = "name,email,phone,address,notes";
const limited = csv.split(",", 3);
console.log(limited);
// ['name', 'email', 'phone']
// Limit of 0 returns empty array
console.log("hello".split("", 0));
// [Global_Objects::eval]
// Array may have fewer elements than limit if string ends early
console.log("ab".split("", 5));
// ['a', 'b']
Working with capture groups
When using a regular expression with capture groups, the captured groups are included in the resulting array:
const text = "2026-03-09";
const parts = text.split(/(-)/);
console.log(parts);
// ['2026', '-', '03', '-', '09']
Common Patterns
Parsing CSV data
const csvRow = "John,Doe,30,New York";
const [firstName, lastName, age, city] = csvRow.split(",");
console.log(firstName, lastName, age, city);
// John Doe 30 New York
Tokenizing user input
const input = " apple banana cherry ";
const tokens = input.trim().split(/\s+/);
console.log(tokens);
// ['apple', 'banana', 'cherry']
Reversing a string
const str = "JavaScript";
const reversed = str.split("").reverse().join("");
console.log(reversed);
// tpircSavaJ
Splitting by multiple delimiters
const data = "apple,banana;cherry|dates";
const fruits = data.split(/[,;|]/);
console.log(fruits);
// ['apple', 'banana', 'cherry', 'dates']
// Handling whitespace around delimiters
const messy = "one , two ; three | four";
const clean = messy.split(/[,;|]/).map(s => s.trim());
console.log(clean);
// ['one', 'two', 'three', 'four']
Parsing query strings
function parseQueryString(query) {
const params = {};
query.split("&").forEach(pair => {
const [key, value] = pair.split("=");
params[decodeURIComponent(key)] = decodeURIComponent(value || "");
});
return params;
}
const query = "name=Ludwig&city=London&country=UK";
console.log(parseQueryString(query));
// { name: 'Ludwig', city: 'London', country: 'UK' }
See Also
- string::slice — Extract a portion of a string
- string::replace — Replace substrings in a string
- array::join — Join array elements into a string