Parse separated values
Convert delimited text such as CSV-like input into individual fields.
JavaScript · Function
String.prototype.split() searches a string for a separator and returns an array of the substrings between matches. The separator may be a string, regular expression, or custom object with Symbol.split; an optional limit caps the result length.
Call split() on a string with a separator to search from left to right and create an array of the text between matches. The separator can be a string, a regular expression, or an object providing Symbol.split; an optional limit caps the number of returned entries.
split(separator)
split(separator, limit)| Name | Type | Description |
|---|---|---|
separator | string | RegExp | object | undefined | The pattern defining where splitting occurs; it may be undefined, a string, or an object with a Symbol.split method. |
limit | number | An optional non-negative integer limiting the number of substrings included in the returned array. |
An Array of strings containing the substrings produced by splitting the string.
const input = "red,green,blue";
const colors = input.split(",");
console.log(colors); // ["red", "green", "blue"]Splits the string at each comma and returns the resulting substrings.
Convert delimited text such as CSV-like input into individual fields.
Break text around flexible patterns, such as whitespace or punctuation.
Extract only an initial portion of a large separated string.
split(separator, limit) returns an array of substrings. It searches from left to right, and separator matches divide the string without appearing as ordinary substring entries.
Follow-up: What can an object separator do? If it supplies Symbol.split, split() calls that method to perform the operation.
separator may be undefined, a string, or an object with Symbol.split, such as a regular expression. Omitting it or passing undefined returns an array containing the original string.
Follow-up: What happens when a separator is a regular expression with captures? The captured groups are included in the returned array.
limit is an optional non-negative integer that bounds the number of array entries. Splitting stops once that many entries have been placed, and remaining text is omitted.
Follow-up: What if the input ends before the limit? The array simply contains fewer entries than the limit.
String.prototype.match()String.prototype.replace()String.prototype.search()Symbol.splitRegExp