I'm having a string like below that i would like to split only on the first ,
. so that if i for instance had following string Football, tennis, basketball
it would look like following array
["football", "tennis, basketball"]
I'm having a string like below that i would like to split only on the first ,
. so that if i for instance had following string Football, tennis, basketball
it would look like following array
["football", "tennis, basketball"]
This should do it
var array = "football, tennis, basketball".split(/, ?(.+)?/);
array = [array[0], array[1]];
console.log(array);
Inspiration: split string only on first instance of specified character
EDIT
I've actually found a way to reduce the above function to one line:
console.log("football, tennis, basketball".split(/, ?(.+)?/).filter(Boolean));
.filter(Boolean)
is used to trim off the last element of the array (which is just an empty string).