Answers for "javascript count occurrences of character in string"

8

javascript count occurrences of letter in string

function count(str, find) {
    return (str.split(find)).length - 1;
}

count("Good", "o"); // 2
Posted by: Guest on May-31-2021
8

count occurrences of character in string javascript

var temp = "This is a string.";
var count = (temp.match(/is/g) || []).length;
console.log(count);

Output: 2

Explaination : 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.
Posted by: Guest on March-04-2020
1

how to count specific letters in string js

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

console.log(("str1,str2,str3,str4".match(new RegExp("str", "g")) || []).length); //logs 4
Posted by: Guest on September-08-2020

Code answers related to "javascript count occurrences of character in string"

Code answers related to "Javascript"

Browse Popular Code Answers by Language