Reputation: 1053
If I have a long string
8718584449,19630000,24,990,10218828289,840000,3,18,8914098889,2120000,4,108,8720551129,3690000,18,42
and I can get indexes of
8718584449, 10218828289, 8914098889, 8720551129
via the loop, how would I get sub-strings
19630000,24,990
840000,3,18
2120000,4,108
3690000,18,42
from that long string?
Basically, for every 4 numbers how dynamically to get the 2nd, the 3rd and the 4th numbers only if I know the first number of that 4-numbers substring? For instance, if I don't have 10218828289 then I don't want to get 840000,3,18
Upvotes: 1
Views: 758
Reputation: 2612
You can use String.split
, Array.slice
, and Array.join
to avoid regular expressions.
var s = '8718584449,19630000,24,990,10218828289,840000,3,18,8914098889,2120000,4,108,8720551129,3690000,18,42';
s.split(',').slice(1, 4).join(','); // => '19630000,24,990'
s.split(',').slice(5, 8).join(','); // => '840000,3,18'
s.split(',').slice(9, 12).join(','); // => '19630000,24,990'
s.split(',').slice(13, 16).join(','); // => '840000,3,18'
If, for whatever reason, you want to use regular expressions:
var s = '8718584449,19630000,24,990,10218828289,840000,3,18,8914098889,2120000,4,108,8720551129,3690000,18,42';
s.match(/\d+,((?:\d+,){2}(?:\d+))/g); // => ['19630000,24,990', …]
Upvotes: 4