(mainStr.split("str").length - 1) //4
console.log(("str1,str2,str3,str4".match(/,/g) || []).length); //yes, this seems unnecessarly overcomplicated but that's it //this count the commas //result 3