Splitting a String in JavaScript

To split a string in JavaScript, you can use the string.split(separator, limit) method. The split() method splits the given string into an array of strings using "separator" as the delimiter. The second parameter determines how many times to split the string. The string.split() method returns a new array without changing the original string. In addition to the string.split() method, you can split a string using Regular Expressions (see example below). In this Split a String in JavaScript example, we are splitting a string using the split() method. An example of splitting a string using Regular Expressions in JavaScript is shown below. Click Execute to run the JavaScript Split String Example online and see the result.
Splitting a String in JavaScript Execute
const str = 'JavaScript Split String';

console.log(str.split(' '));
Updated: Viewed: 5625 times

What is JavaScript?

JavaScript is a high-level, dynamic, and interpreted programming language widely used for creating interactive and dynamic web pages, web applications, and server-side applications. JavaScript can be run on various platforms, including browsers, servers (Node.js), and standalone applications, making it one of the most versatile programming languages available.

What is a string in JavaScript?

A JavaScript string is a data type that represents a sequence of characters. To define a string in JavaScript, you can use single quotes ('...'), double quotes ("..."), or template literals (`...`). Unicode characters are encoded in JavaScript strings using the "\uXXXX" syntax. JavaScript strings are immutable, meaning their contents cannot be changed once they are created. JavaScript strings can be manipulated and combined using various built-in methods: split, concatenation, contains, trim, and reverse. You can also check if it starts or ends with another string, convert array to string, create a multiline string, and get the length of a string.

How to split a JavaScript string?

You can use the split() method in JavaScript to split a string into an array of substrings. The following is the syntax of split() method:

JavaScript split() Syntax
string.split(separator, limit)

Where:
  • separator (optional): a string or Regular Expression. If no separator is specified, the entire string becomes one array element. If the delimiter is the empty string (''), the split() method will return an array where each character is an array element
  • limit (optional): an integer indicating the number of splits. The rest of the elements will be discarded.

JavaScript Split String Examples

The following are examples of string splitting in JavaScript:

Split string using a space separator

The following is an example of splitting a string by space in JavaScript:

JavaScript Split String by Space Example
const str = 'JavaScript Split String';

console.log(str.split(' '));

// output: ['JavaScript', 'Split', 'String']

Split string using a comma separator

The following is an example of splitting a string using a comma in JavaScript:

JavaScript Split String by Comma Example
const str = 'This,is,a,comma,separator,example';

console.log(str.split(','));

// output: ['This', 'is', 'a', 'comma', 'separator', 'example']

Split multiline string into separate lines

The following is an example of splitting a string with a newline separator in JavaScript:

JavaScript Split Multiline String Example
const str = 'JavaScript\nSplit\nString';

console.log(str.split("\n"));

// output: ['JavaScript', 'Split', 'String']

Split a string into separate characters

The following is an example of splitting a string into separate characters in JavaScript:

JavaScript Split String to Separate Characters Example
const str = 'JavaScriptString';

console.log(str.split(""));

// output: ['J', 'a', 'v', 'a', 'S', 'c', 'r', 'i', 'p', 't', 'S', 't', 'r', 'i', 'n', 'g']

Limiting the number of splits

The following is an example of limiting the number of splits in JavaScript:

JavaScript Limiting the Number of Splits Example
const str = 'JavaScript Split String';

console.log(str.split(' ', 1));

// output: ['JavaScript']

Split string into a specified number of substrings

The following is an example of splitting a string into a specified number of substrings in JavaScript:

JavaScript Split String into Specified Number Example
const str = 'JavaScript,Split,String';

console.log(str.split(",", 2));

// output: ['JavaScript', 'Split']

JavaScript Split String with Regular Expressions

The following are examples of splitting strings using Regular Expressions in JavaScript:

Split string using Regular Expression
JavaScript Split String using Regular Expression Example
const date = "19-02-2022";

console.log(date.split(/[.,\/ -]/));

// output: ['19', '02', '2022']

Split strings by capital letters using Regular Expression

The following is an example of splitting a string by capital letters using the split() method and specifying a Regular Expression as the delimiter:

JavaScript Split Strings by Capital Letters Example
const str = 'JavaScriptSplitString';

console.log(str.split(/(?=[A-Z])/));

// output: ['Java', 'Script', 'Split', 'String']

Split a string using an array of delimiters

The following is an example of splitting a string using an array of delimiters:

Split Strings by Several Delimiters Example
const str = 'JavaScript+Split-String';
const separators = [' ', '\\\+', '-', '\\\(', '\\\)',
'\\*', '/', ':', '\\\?'];

let result = str.split(new RegExp(separators.join('|'), 'g'));
console.log(result);

// output: ['JavaScript', 'Split', 'String']

How to convert an array to a string in JavaScript?

The array.join() method is the opposite of the string.split() method. The main difference between the join() and split() methods is that the join() method is used to concatenate array elements into a string, while the split() method is used to split a string into an array using a delimiter.

JavaScript Convert Array to String Example
const array = ['JavaScript', 'Join', 'String'];

console.log(array.join(' '));

// output: JavaScript Join String

See also