1function count(str, find) {
2 return (str.split(find)).length - 1;
3}
4
5count("Good", "o"); // 2
1var temp = "This is a string.";
2var count = (temp.match(/is/g) || []).length;
3console.log(count);
4
5Output: 2
6
7Explaination : The g in the regular expression (short for global) says to search the whole string rather than just find the first occurrence. This matches 'is' twice.
1console.log(("str1,str2,str3,str4".match(/,/g) || []).length); //logs 3
2
3console.log(("str1,str2,str3,str4".match(new RegExp("str", "g")) || []).length); //logs 4