Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript split() Method

JavaScript split() Method

String Object Reference JavaScript String object

Example

Split a string into an array by spaces:

var str = "How are you doing today?";

var n = str.split(" ");

n The result is:

["How","are","you","doing","today?"]

Try it »


Definition and Usage

split() The method is used to split a string into an array according to a 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 support split() method.


Syntax

string

.split(

separator

,

limit

)

Parameter value

Parameters 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 result array will not exceed this value.

Return Value

Type Description
Array Returns an array of strings. The string is split at separator the specified positions, and in the resultthe separator itself is not included.

Technical Details

JavaScript version: 1.1

More examples

Example

Without passing a separator:

var str = "How are you doing today?";

var n = str.split();

n The result is (no splitting):

["How are you doing today?"]

Try it »

Example

Split by each character (including spaces):

var str = "How are you doing today?";

var n = str.split("");

n The result is:

["H","o","w"," ","a","r","e"," ","y","o","u"," ","d","o","i","n","g"," ","t","o","d","a","y","?"]

Try it »

Example

Use limit Limit the number of returned items:

var str = "How are you doing today?";

var n = str.split(" ", 3);

n The result is:

["How","are","you"]

Try it »

Example

Use a specified character as the separator:

var str = "How are you doing today?";

var n = str.split("o");

n The result is:

["H","w are y","u d","ing t","day?"]

Try it »


String Object Reference JavaScript String object