JavaScript String split()

Total
0
Shares

In this tutorial, we will learn about the JavaScript String split() method with examples.

The JavaScript String split() method splits a string into an ordered list of strings and returns them as an array.

split() Example

// JavaScript Program to illustrate split() function

const text = "Welcome to JavaScript Tutorial - ItsJavaScript";

// split the text with space
let result = text.split(" ");
console.log(result);

// Output: [ 'Welcome', 'to', 'JavaScript', 'Tutorial', '-', 'ItsJavaScript' ]

split() Syntax

The syntax of the JavaScript split() method is as follows.

str.split(separator, limit)

Here the str is the string or the string variable.

split() Parameter

The JavaScript split() method takes two parameters, and both are optional.

1) separator (Optional) – The separator determines where the split needs to happen in a given string. The separator can be a single character, a substring, or it can be a regular expression.

If you do not specify the separator, then by default, the entire string is returned as-is.

2) limit (Optional) – A limit is a non-negative number that specifies the number of substrings it needs to split into. 

If the limit is 0, the split() method returns an empty array.

split() Return Value

The split() method splits the string at each point where the separator matches and returns an array of substrings.

Notes:

  • If separator is found, the separator is removed from the string, and the substrings are returned in an array.
  • If the separator is an array, then that Array is restricted to a String and used as a separator.
  • The split() method does not change the original string.

Let us take a look at some examples of using the JavaScript split() method.

Example 1: JavaScript split() method

In the following example, we use the split() method to split the string into words. If the string and separator are empty, the method returns an empty array, as shown below.

// JavaScript Program to illustrate split() function

const text = "Welcome to JavaScript Tutorial - ItsJavaScript";

// split the text into words using space as delimiter
const substr = text.split(" ");
console.log(substr);

// split the text into words using space as delimiter and limit to 2 substrings
const substr2 = text.split(" ", 2);
console.log(substr2);

// split the text into words using hyphen (-) as delimiter
const substr3 = text.split('-');
console.log(substr3);

//In case of empty string, split() returns empty array
const mystring = ''
console.log(mystring.split())

Output

[ 'Welcome', 'to', 'JavaScript', 'Tutorial', '-', 'ItsJavaScript' ]
[ 'Welcome', 'to' ]
[ 'Welcome to JavaScript Tutorial ', ' ItsJavaScript' ]
[ '' ]

Example 2: JavaScript Split the string into an array of characters

If you want to split the string into an array of characters, you can just pass an empty delimiter, as shown below. 

Note: It is not recommended to use the split() method to split the string into an array as we get improper results with non BMP(non-Basic-Multilingual-Plane) character sets. The methods like .split() and .charCodeAt() only respect the characters with a code point below 65536. The higher code points are represented by a pair of (lower valued) “surrogate” pseudo-characters.

// split the text into array of chars using empty string
console.log("ABCDEFGHIJK".split(''));

// split the text into array of chars using empty string and limit to 3 chars
console.log("ABCDEFGHIJK".split('', 3));

Output

['A', 'B', 'C', 'D','E', 'F', 'G', 'H','I', 'J', 'K']
[ 'A', 'B', 'C' ]

Example 3: JavaScript Splitting a string using a regular expression 

If separator is a regular expression that contains capturing parentheses (``), matched results are included in the array as shown below.

Note:\d matches the character class for digits between 0 and 9.

const sampleText = "Hello 1 World 2 !!!"
console.log(sampleText.split(/(\d)/))

Output

[ 'Hello ', '1', ' World ', '2', ' !!!' ]
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Get notified on the latest articles

You May Also Like
JavaScript Array slice()

JavaScript Array slice()

Table of Contents Hide slice() Exampleslice() Syntaxslice() Parametersslice() Return ValueExample 1: Clone an array using the JavaScript slice() methodExample 2: JavaScript slice() methodExample 3: JavaScript slice() With Negative indexExample 4:…
View Post