JavaScript String split()

Summary: in this tutorial, you’ll learn how to use the JavaScript split() method to split a string into an array of substrings.

Introduction to the JavaScript String split() method

The String.prototype.split() divides a string into an array of substrings:

split([separator, [,limit]]);Code language: JavaScript (javascript)

The split() accepts two optional parameters: separator and limit.

1) separator

The separator determines where each split should occur in the original string. The separator can be a string. Or it can be a regular expression.

If you omit the separator or the split() cannot find the separator in the string, the split() returns the entire string.

2) limit

The limit is zero or positive integer that specifies the number of substrings. The split() method will stop when the number of substrings equals to the limit.

If the limit is zero, the split() returns an empty array. If the limit is 1, the split() returns an array that contains the string.

Note that the result array may have fewer entries than the limit in case the split() reaches the end of the string before the limit.

JavaScript split() examples

Let’s take some examples of using the split() method.

1) Splitting the strings into words example

The following example uses the split() method to split the string into words:

let str = 'JavaScript String split()';
let substrings = str.split(' ');

console.log(substrings);Code language: JavaScript (javascript)

Output:

["JavaScript", "String", "split()"]
Code language: JavaScript (javascript)

Note that space (‘ ‘) has been removed in the substrings.

2) Returning a limited number of substrings example

The following example uses the split() method to divide a string into substrings using the space separator. It also uses the second parameter to limit the number of substrings to two:

let str = 'JavaScript String split()';
let substrings = str.split(' ',2);

console.log(substrings);Code language: JavaScript (javascript)

Output:

["JavaScript", "String"]Code language: JavaScript (javascript)

3) Splitting a string using a regular expression example

The following example uses the split() method to split sentences in a paragraph into sentences:

let paragraph = 'Good Morning! How are you? This is John. John is my friend.';
let sentences = paragraph.split(/[!,?,.]/);
console.log(sentences);Code language: JavaScript (javascript)

Output:

["Good Morning", " How are you", " This is John", " John is my friend", ""]Code language: JavaScript (javascript)

If the regular expression contains capturing parentheses (), the split() method also includes the matched results in the array:

let paragraph = 'Good Morning! How are you? This is John. John is my friend.';
let sentences = paragraph.split(/([!,?,.])/);
console.log(sentences);Code language: JavaScript (javascript)

Output:

["Good Morning", "!", " How are you", "?", " This is John", ".", " John is my friend", ".", ""]Code language: JavaScript (javascript)

Notice that the !, ?, . characters are included in the returned array.

Summary

  • Use the JavaScript String split() to divide a string into an array of substrings by a separator.
  • Use the second parameter (limit) to return a limited number of splits.
Was this tutorial helpful ?