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).
array being ["football", "tennis, basketball", ""]array variable with what I put in the console.log. That's the correct answer. You are not outputting the same value as the above function.array value OP was expecting/asking for, yes