Last updated: Mar 1, 2024
Reading timeยท4 min
Use the String.split()
method with array destructuring to split a string
only on the first occurrence of a character.
The array destructuring syntax will capture the two parts of the string in separate variables.
const str = 'bobby-hadz-com'; const [first, ...rest] = str.split('-'); console.log(first); // ๐๏ธ bobby console.log(rest); // ๐๏ธ ['hadz', 'com'] const remainder = rest.join('-'); console.log(remainder); // ๐๏ธ hadz-com
We used the
String.split()
method to split the string on a hyphen (-
).
We used
array destructuring assignment to
assign the first element of the array to the first
variable.
The rest (...) operator is used to gather the remaining array elements into the
rest
variable.
The last step is to use the
Array.join() method to
join the rest
array with a hyphen separator.
If you have to split a string only of the first occurrence often, define a reusable function.
function splitFirstOccurrence(str, separator) { const [first, ...rest] = str.split(separator); const remainder = rest.join('-'); return {first, remainder}; } const result = splitFirstOccurrence('bobby-hadz-com', '-'); console.log(result); // { first: 'bobby', remainder: 'hadz-com' } console.log(result.first); // bobby console.log(result.remainder); // hadz-com
The function takes a string and a separator as parameters and splits the string only on the first occurrence of the separator.
If you only need to get the first part after splitting, set the limit
argument
to 1
.
const arr = 'bobby-hadz-com'.split('-', 1); console.log(arr); // ๐๏ธ [ 'bobby' ] console.log(arr[0]); // ๐๏ธ bobby
The String.split() method takes a separator and splits the string into an array on each occurrence of the provided delimiter.
The String.split()
method takes the following 2 parameters:
Name | Description |
---|---|
separator | The pattern describing where each split should occur. |
limit | An integer used to specify a limit on the number of substrings to be included in the array. |
When the limit
argument is set to 1
, only the part before the first
occurrence of the delimiter is contained in the array.
Alternatively, you can use the slice()
method.
The slice()
method will return the portion of the string after the first
occurrence of the character.
const str = 'bobby-hadz-com'; const after = str.slice(str.indexOf('-') + 1); console.log(after); // ๐๏ธ hadz-com const before = str.slice(0, str.indexOf('-')); console.log(before); // ๐๏ธ bobby
The String.slice() method extracts a section of a string and returns it, without modifying the original string.
The String.slice()
method takes the following arguments:
Name | Description |
---|---|
start index | The index of the first character to include in the returned substring |
end index | The index of the first character to exclude from the returned substring |
When only a single argument is passed to the String.slice()
method, the slice
goes to the end of the string.
The first example shows how to get the part after the last occurrence of a character.
const str = 'bobby-hadz-com'; const after = str.slice(str.indexOf('-') + 1); console.log(after); // ๐๏ธ hadz-com
1
to its index to start immediately after.The second example shows how to get the part before the first occurrence of the character.
const str = 'bobby-hadz-com'; const before = str.slice(0, str.indexOf('-')); console.log(before); // ๐๏ธ bobby
The slice starts at index 0
and goes up to, but not including the index of the
separator.
You can also split a string on the first occurrence of a character by passing a
regex to split()
.
const str = 'bobby-hadz-com'; const arr = str.split(/-(.*)/); console.log(arr); // ๐๏ธ [ 'bobby', 'hadz-com', '' ] const [before, after] = arr; console.log(before); // ๐๏ธ bobby console.log(after); // ๐๏ธ hadz-com
The forward slashes / /
mark the beginning and end of the regular expression.
The regex is in the form of /{char_to_split_on}(.*)/
.
The parentheses are called a capturing group.
The dot .
is a special character and matches any single character.
The asterisk *
character matches the preceding item (any character) 0 or more
times.
When we split with a capturing group, the matched results are returned in the array.
You can use destructuring assignment to assign the first two array elements to variables or access the array at a specific index.
const str = 'bobby-hadz-com'; const arr = str.split(/-(.*)/); console.log(arr); // ๐๏ธ bobby console.log(arr[0]); // ๐๏ธ bobby console.log(arr[1]); // ๐๏ธ hadz-com
JavaScript indexes are zero-based, so the first element in an array has an index
of 0
and the last element has an index of array.length - 1
.
You can learn more about the related topics by checking out the following tutorials: