How do I split a string in JavaScript?

To split a string in JavaScript, you can use the string.split(separator, limit) method. The split() method is used to split the given string into an array of strings using the "separator" as a delimiter. The second parameter determines how many times to divide the matching value. If no limit is specified, the string will be split into all matching values. The split() method returns a new array without changing the original string. Besides the split() method, you can split a string using Regular Expressions. 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.
How do I split a string in JavaScript? Execute
let str = 'JavaScript Split String';

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

Syntax of the Split Method in JavaScript

Following is the syntax of split() method:

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

Where:
  • 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';

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

// 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';

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

// 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';

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

// 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';

console.log(str.split(/[\\\/]/));

// 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'));
console.log(result);

// 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'];

console.log(array.join());

// output: JavaScript,Join,String

See also