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

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

Javascript count instances of character in a string

 function countInstancesOf(letter, sentence) {
  var count = 0;

  for (var i = 0; i < sentence.length; i++) {
    if (sentence.charAt(i) === letter) {
      count += 1;
    }
  }
  return count;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery post json example 
Javascript :: Add click event to querySelectorAll js 
Javascript :: get all mondays in calendar+js 
Javascript :: UnhandledPromiseRejectionWarning: Error: Node is either not clickable or not an HTMLElement 
Javascript :: js detect screen size change 
Javascript :: javascript wait 1 second 
Javascript :: react font awesome delete icon 
Javascript :: navigate to url javascript 
Javascript :: remove word from string javascript 
Javascript :: error java.io.filenotfoundexception tessdata/eng.traineddata 
Javascript :: get data in date filter sequelize 
Javascript :: javascript canvas touchmove 
Javascript :: random id generator 
Javascript :: import reactdom 
Javascript :: load a new page in javascript 
Javascript :: mouse coordinates not math with canvas coordinates in js 
Javascript :: how to generate a fibonacci sequence in javascript 
Javascript :: boton de copiar en html y js 
Javascript :: jquery check if string contains specific word 
Javascript :: read json file flutter 
Javascript :: express request path 
Javascript :: adding delay in javascript foreach loop 
Javascript :: remove div javascript 
Javascript :: m- m sequelize 
Javascript :: js hex 
Javascript :: React + modal: input field auto focus 
Javascript :: react router dom 6 go back 
Javascript :: jquery check input 
Javascript :: angular reactive form remove validation 
Javascript :: jquery insert option into select 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =