count characters in typescript
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.let counter = str => {
return str.split('').reduce((total, letter) => {
total[letter] ? total[letter]++ : total[letter] = 1;
return total;
}, {});
};
counter("aabsssd"); // => { a: 2, b: 1, s: 3, d: 1 }function count (string) {
var count = {};
string.split('').forEach(function(s) {
count[s] ? count[s]++ : count[s] = 1;
});
return count;
}
Also in JavaScript:
- nested array loop in javascript
- Material-ui clock icon
- bootstrap cdn for jquery
- ajax laravel get values from form
- php generate random string fixed length
- [JsonConverter(typeof(StringEnumConverter))] on list of enums
- javascrip check if string contains substring
- javascript sum of array
- react axios download zip file
- oops in javascript
- error: Error: Unable to resolve module `react-native-gesture-handler` from `node_modules\@react-navigation\native\lib\module\Scrollables.js`: react-native-gesture-handler could not be found within the project.
- open a new tab when clicking on a link react
- how to make a rectangle in matter.js
- nodejs make directory
- declare an object in jquery\
- sum elements in list with same name js
- toggle class in javascript
- regex to get part of word nodejs
- javascript find prime number
- JavaScript after
- javascript number to string
- ejemplo async await javascript
- forEach
- date angular