String.prototype.split()
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']
A plain string separator works well for simple delimiters like spaces. When the separator is a regular expression, you can split on patterns instead of fixed strings — for example, commas or character classes — which is often more practical for real-world data.
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']
A regex separator opens up flexible patterns, but you may not always want every piece that the split produces. The limit argument caps the output array to a maximum length, which is useful when working with long strings where only the first few segments matter.
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));
// []
// Array may have fewer elements than limit if string ends early
console.log("ab".split("", 5));
// ['a', 'b']
Limiting the result prevents runaway array creation on unexpectedly long input. Another way the separator shapes the output is through capture groups — wrapping part of a regex separator in parentheses includes that matched text in the result array, which lets you preserve delimiters alongside the split pieces.
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']
The examples above cover the core mechanics of split. The patterns below show how those mechanics translate into everyday tasks — parsing CSV rows, cleaning up whitespace, extracting query parameters, and other operations where splitting is the natural first step.
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
CSV parsing with destructuring is a quick way to pull named values from a row. When your input is less structured — user-entered text with irregular whitespace — a regex separator with trim() cleans things up before you process the tokens.
Tokenizing user input
const input = " apple banana cherry ";
const tokens = input.trim().split(/\s+/);
console.log(tokens);
// ['apple', 'banana', 'cherry']
Tokenizing strips noise and normalises the input into clean, usable pieces that are ready for further processing. That same split-then-process pattern also powers the classic string-reversal trick, where you split into characters, reverse the array, and join back together.
Reversing a string
const str = "JavaScript";
const reversed = str.split("").reverse().join("");
console.log(reversed);
// tpircSavaJ
Reversing with split("") is convenient but has a Unicode pitfall that we cover in detail below. When your data uses multiple delimiters — say commas, semicolons, and pipes mixed together — a character-class regex handles them all in a single pass without extra work.
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']
Multiple-delimiter splitting often pairs with a map to trim or normalise each piece. When the data is structured as key-value pairs — like URL query strings — a two-level split extracts every parameter into an object for easy lookup.
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' }
split() and empty strings
Splitting on an empty string ("") is a fast way to get individual characters, but it has an important edge case with Unicode: it splits on UTF-16 code units, not Unicode code points. Emoji and characters above U+FFFF are represented as surrogate pairs and will be split into two broken pieces. Use the spread operator or Array.from() instead for reliable Unicode-safe character splitting:
"hello".split(""); // ['h', 'e', 'l', 'l', 'o'] — fine for ASCII
"😀".split(""); // ['\\uD83D', '\\uDE00'] — broken surrogate pair
[..."😀"]; // ['😀'] — correct
Splitting with capture groups
When the separator is a regex with capture groups, the captured text is included in the output array between each split piece. This is useful when you need to preserve the delimiters in the result, such as when tokenizing code or formatting strings:
"one+two-three".split(/([-+])/);
// ['one', '+', 'two', '-', 'three']
Without the capture group, the delimiters would be discarded and only ['one', 'two', 'three'] would remain.
split() vs match()
split() divides a string and returns the non-matching parts (with separators discarded unless captured). match() with the g flag does the opposite: it returns only the matching parts. If you need to extract all occurrences of a pattern rather than everything between matches, matchAll() or match(/pattern/g) is the right tool.
When split() is the better fit
split() is the better fit when you want to turn one string into a list of parts for later processing. That makes it the natural choice for CSV-style data, simple tokenization, and any case where the separator is more important than the matched text itself.
It is also a good match for pipelines that immediately map, trim, or filter the pieces. Because the result is already an array, the next step can stay focused on the data rather than on the mechanics of finding separators.
See Also
- String.prototype.slice() — Extract a portion of a string
- String.prototype.replace() — Replace substrings in a string
- Array.prototype.join() — Join array elements into a string