Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript count occurrences of letter in string

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

count("Good", "o"); // 2
Comment

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.
Comment

javascript count occurence of character in string

// String Prototype: myCount = myString.polycount("countThis")
String.prototype.polycount = function (criteria) {
  return this.split(criteria).length - 1 }
// Arrow Function (STR): myCount = strCount(myString, "countThis")
const strCount = (stack, find) => stack.split(find).length - 1
// Array Prototype: myCount = myArray.polycount("countThis")
Array.prototype.polycount = function (criteria) {
  return this.join("").split(criteria).length - 1 }
// Arrow Function (ARR): myCount = arrCount(myArray, "countThis")
const arrCount = (stack, find) => stack.join("").split(find).length - 1
Comment

PREVIOUS NEXT
Code Example
Javascript :: node.js workflow template 
Javascript :: go over each line in text nodejs 
Javascript :: convert space to dash/hyphen javascript regex 
Javascript :: hide component blur react hooks 
Javascript :: rewrite /src/App.js 
Javascript :: return where an property eqauls 
Javascript :: why is javascript the worst programming language 
Javascript :: vue-jstree 
Javascript :: modal nodejs 
Javascript :: javascript onclick event add html element 
Javascript :: Chrome Extension get Selection 
Javascript :: pragmatic view on the meaning of life 
Javascript :: nuxt two props 
Javascript :: how to change selected link in jquery 
Javascript :: node js validate body without middleware 
Javascript :: how to read json in c# and insert into database 
Javascript :: rest in object destructuring 
Javascript :: calling computed function inside methods 
Javascript :: Immediate execution of a function 
Javascript :: Collision between two div vanillaJS no detection 
Javascript :: popover not working when next page datatable 
Javascript :: bootstrapmaterialdatepicker get selected value on changes 
Javascript :: How to Subtract the numbers in the array, starting from the left in javascript 
Javascript :: mengakses gambar didalam asset angular 
Javascript :: Find speacific object from an array in javascript 
Javascript :: template literals multiline js 
Javascript :: select not input elements text JS 
Javascript :: react native password field 
Javascript :: network information api js 
Javascript :: how to replace multiple characters in url in jqury 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =