0

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

JavaScript
console.log(("str1,str2,str3,str4".match(/,/g) || []).length); // 3

console.log(("str1,str2,str3,str4".match(new RegExp("str", "g")) || []).length); // 4
RUN
add comment
Suggest a different solution or add another example