For example, i have this string:
JavaScript
x
2
1
1:55520:2:THE LIGHTNING ROAD:5:3:6:28762:8:10:9:10:10:44654418:12:3:13:21:14:2440639:17:1:43:3:25::18:10:19:12:42:0:45:3527:3:UmVtb3ZlZCBDb2lucywgfiBUaW1lbGVzcyBSZWFsIC8gUmVkdWxvYw==:15:3:30:55520:31:0:37:0:38:0:39:10:46:1:47:2:35:0#28762:timeless real:6805706##9999:0:10#9c3be04b52b77197134e199989920f4aa55b933b
2
And I want to split everything in it by every second colon (:
). How can I do it?
Required Output
JavaScript
1
31
31
1
[
2
"1:55520",
3
"2:THE LIGHTNING ROAD",
4
"5:3",
5
"6:28762",
6
"8:10",
7
"9:10",
8
"10:44654418",
9
"12:3",
10
"13:21",
11
"14:2440639",
12
"17:1",
13
"43:3",
14
"25:",
15
"18:10",
16
"19:12",
17
"42:0",
18
"45:3527",
19
"3:UmVtb3ZlZCBDb2lucywgfiBUaW1lbGVzcyBSZWFsIC8gUmVkdWxvYw==",
20
"15:3",
21
"30:55520",
22
"31:0",
23
"37:0",
24
"38:0",
25
"39:10",
26
"46:1",
27
"47:2",
28
"35:0#28762",
29
"timeless real:6805706##9999",
30
"0:10#9c3be04b52b77197134e199989920f4aa55b933b"
31
]
Advertisement
Answer
Sure. You just have to use in-built function .split( )
. Here if you have str = "Hi there Go" then on
str.split(‘ ‘)you will get
[‘Hi’,’there’,’Go’]` You can do the same in your case.
JavaScript
1
13
13
1
let str ="1:55520:2:THE LIGHTNING ROAD:5:3:6:28762:8:10:9:10:10:44654418:12:3:13:21:14:2440639:17:1:43:3:25::18:10:19:12:42:0:45:3527:3:UmVtb3ZlZCBDb2lucywgfiBUaW1lbGVzcyBSZWFsIC8gUmVkdWxvYw==:15:3:30:55520:31:0:37:0:38:0:39:10:46:1:47:2:35:0#28762:timeless real:6805706##9999:0:10#9c3be04b52b77197134e199989920f4aa55b933b";
2
3
let elem = str.split(':');
4
let arr = [];
5
for(let i =0;i<elem.length;i++)
6
{
7
if(i%2!=0)
8
{
9
arr.push(elem[i-1]+`:`+elem[i]);
10
}
11
}
12
13
console.log(arr)