Splitting strings 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 strings in JavaScript Execute
let str = 'JavaScript Split String';

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

Syntax of the Split Method in JavaScript

Following is the syntax of split() method:

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

  • separator (optional): the string or Regular Expression is used to split the string. If no delimiter is specified, the entire string becomes one array element. If the separator is the empty string (''), each character of the string is separated
  • limit (optional): an integer indicating the number of splits in the given string. If a string remains unchecked after the limit is reached, it is not displayed in the array

Split Method Examples in JavaScript

Below you can see more examples splitting a string in JavaScript:

Split string to character array

JavaScript Split String to Characters Example
let str = 'JavaScript';


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

Split string by space

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

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

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

Limiting the number of splits

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

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

// output: ['JavaScript']

Split string using a comma separator

JavaScript Split String with Comma Separator Example
let str = 'This,is,a,comma,separator,example';


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

Split string using Regular Expression

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

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

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

Split strings by capital letters using Regular Expression

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


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

Split string using a slash as a separator

JavaScript Split String with Slash Example
let str = 'This/is/slash/example';


// output: ['This', 'is', 'slash', 'example']

Split a string using an array of different delimiters

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

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

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

How to convert array to 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 Array Join Example
let array = ['JavaScript', 'Join', 'String'];


// output: JavaScript,Join,String

See also