EN
JavaScript - split only 2 occurrences of the string
0 points
In this article, we would like to show you how to split only 2 occurrences of the string in JavaScript.
Quick solution:
xxxxxxxxxx
1
const text = 'This is example text...';
2
const words = text.split(' ', 2); // ['This', 'is']
In this example, we use the split()
method with the second (optional) argument - limit
, to split only two occurrences of the text
string.
xxxxxxxxxx
1
const text = 'This is example text...';
2
const limit = 2;
3
4
const words = text.split(' ', limit); // ['This', 'is']
5
6
console.log(words[0]); // This
7
console.log(words[1]); // is
8
9
console.log(words[2]); // undefined
Output:
xxxxxxxxxx
1
This
2
is
3
undefined