myString.split( [ separator ], [ limit ] ) in ECMAScript 262

Separate the string into pieces and return an array of the resulting substrings.

Arguments

nametypedescription
separator Object [optional] Either a String or Regular Exp
limit Number [optional] The maximum number of substrin
Return Type
Array

Description

The pieces of the string matched by separator are not included in the results. The following example takes a comma-delimited string and turns it into an array of strings:

var names = "Gavin,Bjornar,Thayer,Lisa,Linden,Chandra";
names = names.split( ',' );
// ** names is now an array of strings:
// ** ['Gavin','Bjornar','Thayer','Lisa','Linden','Chandra']

If limit is supplied, the resulting array will be truncated to have no more elements than specified by limit:

var names = "Gavin,Bjornar,Thayer,Lisa,Linden,Chandra";
names = names.split( ',' , 3 );
// ** names is now ['Gavin','Bjornar','Thayer']

The string specified by separator does not have to be a single character:

var names = "Gavin :: Bjornar :: Thayer :: Lisa :: Linden";
names = names.split(' :: ');
// ** names is now ['Gavin','Bjornar','Thayer','Lisa','Linden','Chandra']

Specifying a regular expression for separator allows you to split the string based on a variety of criteria:

var sentence = "It's the end of the world as we know it, and I feel fine.";
var words = sentence.split(/[\s.,]+/);
// ** words is ["It's","the","end","of","the","world","as","we","know","it","and","I","feel","fine",""]

Note in the above that there is an empty string at the end of the array. This is because the final period was included as a separator by the regular expression.

If the regular expression used to split the string has captured subexpressions in it, the captured text is included in the split results

var html = "Hello <b>World</b>, how <em>are</em> you?";
var code = html.split( / *<(\/?[^>]+)>,? */ );
// ** code is ['Hello','b','World','/b','how','em','are','/em','you?']

If separator is an empty string, then an array is returned of every character in the original string:

var msg = "Hello World";
var letters = msg.split('');
// ** letters is ['H','e','l','l','o',' ','W','o','r','l','d']

Finally, if separator is omitted, a single-element array with the source string in it is returned:

var msg = "Hello World";
var result = msg.split();
// ** result is ['Hello World']