Javascript Reference - JavaScript String split() Method








split() separates the string into an array of substrings based on a separator. The separator may be a string or a RegExp object.

An optional second argument sets the array limit. It ensures that the returned array will be no larger than a certain size.

If an empty string ("") is used as the separator, the string is split for each character.

The split() method does not change the original string.

Browser Support

split() Yes Yes Yes Yes Yes




Syntax

stringObject.split(separator, limit);

Parameter Values

Parameter Description
separator Optional. separator string. If omitted, the entire string is returned.
limit Optional. An integer limits the number of splits

Return Value

An array, containing the splitted values.

Example


var colorText = "A,B,C,D"; 
var colors1 = colorText.split(","); //["A", "B", "C", "D"] 
console.log(colors1);
var colors2 = colorText.split(",", 2); //["A", "B"] 
console.log(colors2);
var colors3 = colorText.split(/[^\,]+/); //["", ",", ",", ",", ""] 
console.log(colors3);

The code above generates the following result.