Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

String split in javascript after each second comma

I am having this string:

16First,Second,18Third,Fourth,25Fifth,Sixth,38Seven,Eight

I want to split the string in a way that it will produce and array containing each element after the second comma, basically

16First,Second,(split here)18Third,Fourth,(split here)25Fifth,Sixth,(split here)38Seven,Eight

So the final output would be an array like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Array[1] = 16First,Second,
Array[2] = 18Third,Fourth,
Array[3] = 25Fifth,Sixth,
Array[4] = 38Seven,Eight

And so on no matter how long the string is. I am confused because it needs to split on each 2nd comma and there isn’t ( or at least I haven’t find any guide/solution about it ).

>Solution :

I would use a regex match all approach here with the help of match():

var input = "16First,Second,18Third,Fourth,25Fifth,Sixth,38Seven,Eight";
var matches = input.match(/[^,]+,[^,]+/g);
console.log(matches);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading