In this tutorial, you will learn to split a string when a certain substring occurs within the string.
The split() method divides a string into an ordered list of substrings, then puts these substrings in an array and returns the array.
The method divides the string and removes the separator when it finds a matching pattern that is specified. The pattern is passed as the first parameter of the split method.
The pattern can be a simple string or a regular expression.
const str = "To do or not to do."; // splitting string from wherever is a space const subStrings = str.split(' '); // array containing all substrings console.log(subStrings); // ["To","do","or","not","to","do."]
In the above example, the split method divides the string into substring from wherever there is a space, then put that substring into an array and return the array as you can see in the output of the above example.
Syntax of split method
The syntax of split() method is as follows:
string.split([separator [ ,endIndex]]);
The split method accepts 2 arguments of both of which are optional and is used to control when to cut the string into substring and how many substrings to return in the array. Let's see both arguments.
separator (optional) - This is the first argument of the split method. It is the pattern that describes where the split should occur in the string. The separator can be a string or a regular expression.
- When the separator is an empty string ('') then the string is split at each of its UTF-16 characters.
- In case there is more than one character in the separator then the entire character sequence must be found in order to split.
- If the separator does not match any sequence or is omitted in the split method then the array is returned with one element as an entire string.
- If the separator matches at the beginning or end of the string then the resulting array has the first or last element as an empty string depending on where the separator appears.
limit (optional) - It a positive integer that specifies the limit of how many substrings the returning array can include. When the limit is provided then the split method stops entries of separated substring into the array when the limit reached. If the limit is 0 then an empty array () is returned.
Let's see some useful example of splitting a string, here we will cover every possible case for a split method with its separator and the limit.
# Example 1: Using regex as separator
The split method accepts both string and regex as a separator. The following example uses the regular expression as a pattern to find the match and split the string.
const str = "Exercise1Exercise2Exercise3to be done"; // splitting at number or spaces using regex const subStrings = str.split(/[0-9, ]/); // [ "Exercise", "Exercise", "Exercise", "to", "be", "done" ] console.log(subStrings);
In the above example, a regular expression is used as a separator which split the string wherever there is an integer or space.
# Example 2: Using limit in split method
The limit is the second parameter of the split method which control how many entries can be made in the resulting array by the method.
const str = "To do or not to do"; // splitting at spaces const subStrings = str.split(" ", 3); // ["To","do","or"] console.log(subStrings);
# Example 3: Omitting separator
When the separator is omitted then the entire string is included in the returning array as the only member of the array.
const str = "To do or not to do"; const words = str.split(); console.log(words);
# Example 4: No match found in string
When the separator match no string sequence in the string then the entire string is included in the returning array as the only member of the array same as if the separator is omitted.
const str = "To do or not to do"; const words = str.split("work"); // no match console.log(words);
# Example 5: Match found at beginning or end
When the separator match at the beginning or at the end of the string then an empty string is included as a representation of separation at the beginning or at the end of the returning array.
const str = " To do or not to do "; const words = str.split(" "); // match at beginning console.log(words);
# Example 6: Remove %20 from URL
Use the split method to split the string at %20 into an array of strings, then use the join() method to joint the strings of the array by using dash (-) as the new separator.
Points to remember:
- The second parameter limit controls the number of array elements in returning the array from the method.