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.function count (string) {
var count = {};
string.split('').forEach(function(s) {
count[s] ? count[s]++ : count[s] = 1;
});
return count;
}function charCount(myChar, str) {
let count = 0;
for (let i = 0; i < str.length; i++)
if (str.charAt(i) == myChar)
count++
return count;
} const recorrences = ['a', 'b', 'c', 'a', 'b','a']
.map(i => !!~i.indexOf('a'))
.filter(i => i)
.length;
console.log(`recorrences ${recorrences}`)
//recorrences 3
Also in JavaScript:
- for loop inside a for loop javascript
- passing data in react router history,push
- get domain name javascript
- javascript dice throw
- <!DOCTYPE html> <!--[if IE 9]> <html lang="" class="js ie9 ... www.tork.cz › product-list www.tork.cz › product-list Cached addEventListener(e,a,!1)})}l(c)}},{}],3:[function(e,n,t){function r(e,n){if(!i)return!1 ... typeof window.performance
- difference between React Native and React
- express img folder
- props and state react
- how to use a js class in react
- Math.ceil
- add items to a list in a document monoose
- nodejs copy and rename file
- prepend javascript
- js fake await
- javascript document.createElement add function
- Uncaught SyntaxError: Cannot use import statement outside a module three.js
- how to add multiple comment in react
- vscode angular: running ngcc
- update table remove a key from json object mysql
- get location from brwoser react
- javascript generate a random number between two numbers thats not 1
- node save json to file
- jquery visible
- javascript get last character in string