JavaScript String split() Method

The String split() method is used to split a string into an array of substrings using a specified separator provided in the argument. This method does not change the original string.

Syntax

string.split(separator, limit)

Parameters

Parameter Description
separator(optional) Specifies the character, or the regular expression, to use for splitting the string.
If omitted, the entire string will be returned as the sole element of an array.
limit(optional) An integer that specifies a limit on the number of splits to be found. 

Return value

It returns a new array of strings formed after splitting a given string at each point where the separator occurs.

Visual Representation

Visual Representation of JavaScript String split() Method

Example 1: How to Use String Split () Method

let text = "Browse appdividend.com and enjoy coding"; 
let result = text.split(" "); 
console.log(result);

Output

[ 'Browse', 'appdividend.com', 'and', 'enjoy', 'coding' ]

In this example, the space character ” “ is used as the separator, so the resulting array contains each word from the text as an individual element.

Example 2: Split the string with a letterVisual Representation of Split the string with a letter

let text = "Browse appdividend.com and enjoy coding";
let result = text.split("appdividend");
console.log(result);

Output

[ 'Browse ', '.com and enjoy coding' ]

In this example, it splits the sentence into two parts using ‘appdividend‘ as the separator, resulting in the array: [ ‘Browse ‘, ‘.com and enjoy coding’ ].

Example 3: Splitting a string with a LimitVisual Representation of Splitting a string with a Limit

let text = "Browse appdividend.com and enjoy coding"; 
let result = text.split(" ", 3); 
console.log(result);

Output

[ 'Browse', 'appdividend.com', 'and' ]

In the above example, we split the text into words using the space ‘ ‘ as a separator. The limit is set to 3, resulting in an array containing only the first three words.

Example 4: Splitting a string by each characterVisual Representation of Splitting a string by each character

To split a string by each character, you can use an “empty string (””) as the separator argument.

let text = "appdividend"; 
let result = text.split(""); 
console.log(result)

Output

[
 'a', 'p', 'p', 'd',
 'i', 'v', 'i', 'd',
 'e', 'n', 'd'
]

Example 5: Splitting a string using regexVisual Representation of Splitting a string using regex

JavaScript split regex uses a regular expression as the separator argument for the split() method. A regular expression is a pattern that can match various combinations of characters in a string.

let text = "appdividend-is-a-blog"; 
let result = text.split(/[^a-zA-Z]/); 
console.log(result);

Output

[ 'appdividend', 'is', 'a', 'blog' ]

Example 6: Splitting a String Using a Non-matching CharacterVisual Representation of Splitting a String Using a Non-matching Character

let text = "appdividend-is-a-blog"; 

let result = text.split('*');

console.log(result)

Output

[ 'appdividend-is-a-blog' ]

In this example, the method is applied to the string “appdividend-is-a-blog” with the asterisk * as the separator. Since the asterisk does not appear in the string, the entire string is returned as a single element in an array.

Example 7: No SeparatorVisual Representation of No Separator

let text = "appdividend-is-a-blog"; 
let result = text.split(); 
console.log(result);

Output

[ 'appdividend-is-a-blog' ]

In this example, we use the string ‘appdividend-is-a-blogwithout a separator, resulting in an array containing the entire string as a single element.

Browser compatibility

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Opera 3 and above
  • Safari 1 and above 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.