JavaScript split()Method
Example
Split the string into an array by spaces:
var str = "How are you doing today?";
var n = str.split(" ");
var n = str.split(" ");
nThe result is:
["How","are","you","doing","today?"]
Try it Yourself »
Definition and Usage
split()This method is used to split a string into an array according to the specified separator.
Tip:If an empty string("")is used as the separator, the string will be split intoan array of individual characters。
Note: split()It does not modify the original string, but returns a new array.
Browser Support
All major browsers supportsplit()the method.
Syntax
string.split(separator, limit)
Parameter Values
| Parameter | Description |
|---|---|
| separator | Optional. A string or regular expression used to specify the split position. For example: " "(space),","、/\d/etc. |
| limit | Optional. Limits the maximum length of the returned array. If this parameter is set, the number of elements in the resulting array will not exceed this value. |
Return Value
| Type | Description |
|---|---|
| Array | Returns an array of strings. The string is split atseparatorthe specified positions, and the resultdoes not contain the separator itself。 |
Technical Details
| JavaScript Version: | 1.1 |
|---|
More Examples
Example
Without passing a separator:
var str = "How are you doing today?";
var n = str.split();
var n = str.split();
nThe result is (no splitting):
["How are you doing today?"]
Try it Yourself »
Example
Split by each character (including spaces):
var str = "How are you doing today?";
var n = str.split("");
var n = str.split("");
nThe result is:
["H","o","w"," ","a","r","e"," ","y","o","u"," ","d","o","i","n","g"," ","t","o","d","a","y","?"]
Try it Yourself »
Example
Uselimitto limit the number of returned items:
var str = "How are you doing today?";
var n = str.split(" ", 3);
var n = str.split(" ", 3);
nThe result is:
["How","are","you"]
Try it Yourself »
Example
Use a specified character as the separator:
var str = "How are you doing today?";
var n = str.split("o");
var n = str.split("o");
nThe result is:
["H","w are y","u d","ing t","day?"]
Try it Yourself »
Other Extensions
JavaScript String Object