Count the number of occurrences of a character in a string in Javascript

I need to count the number of occurrences of a character in a string.

For example, suppose my string contains:

var mainStr = "str1,str2,str3,str4";

I want to find the count of comma , character, which is 3. And the count of individual strings after the split along comma, which is 4.

I also need to validate that each of the strings i.e str1 or str2 or str3 or str4 should not exceed, say, 15 characters.

37 s
37

Leave a Comment