Simply, use the split to find out the number of occurrences of a character in a string.
mainStr.split(',').length
// gives 4 which is the number of strings after splitting using delimiter comma
mainStr.split(',').length - 1
// gives 3 which is the count of comma