JavaScript String split() method
- Previous page slice()
- Next Page startsWith()
- Go to the Previous Level JavaScript String Reference Manual
Definition and usage
split()
The method splits the string into an array of substrings.
split()
The method returns a new array and does not change the original string.
If (" ") is used as a delimiter, the string is split between words.
See also:
Example
Example 1
Split words
let text = "How are you doing today?"; const myArray = text.split(" ");
Example 2
Split words and return the second word:
let text = "How are you doing today?"; const myArray = text.split(" "); let word = myArray[1];
Example 3
Split characters, including spaces:
const myArray = text.split(" ");
Example 4
Using the limit parameter:
const myArray = text.split(" ", 3);
Example 5
Split the string into characters and return the second character:
const myArray = text.split(" ");
Example 6
Using a letter as a delimiter:
const myArray = text.split("o");
Example 7
If the separator parameter is omitted, it returns an array containing the original string:
const myArray = text.split();
Syntax
string.split(',separator, limit)
Parameter
Parameter | Description |
---|---|
separator |
Optional. The string or regular expression to be used for splitting. If omitted, it returns an array containing the original string. |
limit |
Optional. An integer to limit the number of splits. Items exceeding the limit are excluded. |
Return value
Type | Description |
---|---|
Array | An array containing the split values. |
Technical details
Parameter separator
This parameter is a string or a regular expression, splitting from the specified location string.
Parameter limit
This optional integer specifies the maximum length of the returned array. If this parameter is set, the returned substrings will not exceed the number specified by this parameter. If the parameter is not set, the entire string will be split regardless of its length.
Return value
Return a string array. The array is returned by splitting in separator Split the string at the specified boundary string Split into substrings created. The substrings in the returned array do not include separator itself.
However, if separator If it is a regular expression including subexpressions, then the returned array includes substrings that match these subexpressions (but does not include the text that matches the entire regular expression).
Description
split()
This method will create and return a string array, where the elements are the specified strings string with a maximum length of limit times. These substrings are created by searching for substrings in the string from start to end that match separator matches the text, split before and after the matching text string obtained. The returned substrings do not include the delimiter text (except for the cases mentioned at the end of this section). If the delimiter matches from the beginning of the string, the first element of the returned array is an empty string, which is the text before the delimiter. Similarly, if the delimiter matches the end of the string, the last element of the returned array is also an empty string (assuming limit there is no conflict).
If not specified separator, then it is fundamentally wrong string perform the split, the returned array will have only one element, and the string elements will not be split. If separator is an empty string or matches an empty string, then string Each character between them will be split, and the length of the returned array is equal to the length of the string (assuming limit is not less than this length) (note that this is a special case because there is no empty string before the first character and after the last character that matches).
As mentioned before, the substrings in the array returned by this method do not include the delimiter text used to split the string. But if separator If it is a regular expression including subexpressions, then the returned array includes substrings that match these subexpressions (but does not include the text that matches the entire regular expression).
Note:string.split()
Perform the operation with Array.join()
Perform the opposite operation.
Browser support
split()
Is ECMAScript1 (ES1) feature.
All browsers fully support ES1 (JavaScript 1997):
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support | Support |
- Previous page slice()
- Next Page startsWith()
- Go to the Previous Level JavaScript String Reference Manual