Reputation: 3
Im inputting some csv data such as S:1111 S:2222 S:3333 S:4444 S:5555 S:6666 S:7777 S:8888
and im trying to parse the data in a way so that it will be aligned in as 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888
. But when i try to parse the data, i get something like 1111 2222 3333 4444 5555 6666 7777 8888
.
var spaces = 4;
var csv = 'S:1111 S:2222 S:3333 S:4444 S:5555 S:6666 S:7777 S:8888';
var output =
csv.split(' '.repeat(spaces)).map(part => part.substr(2)).join(' '.repeat(spaces));
console.log(output);
Does anyone see the reason why i could be getting the output the way i am getting it instead of the way that i want it?
Upvotes: 0
Views: 55
Reputation: 171679
You could use a regex that looks for characters between the :
and space or end of line then join the matches
const expr =/(?<=\:).+?(?=\s|$)/g;
const str ='S:1111 S:2222 S:3333 S:4444 S:5555 S:6666 S:7777 S:8888'
const res = str.match(expr).join(', ')
console.log(res)
Upvotes: 1
Reputation: 23654
This matches your desired output
var spaces = 4;
var csv = 'S:1111 S:2222 S:3333 S:4444 S:5555 S:6666 S:7777 S:8888';
var output =
csv.split(/\s+/).map(part => part.split(':')[1]).join(', ');
console.log(output);
Upvotes: 1