I have been trying to solve a puzzle of counting characters in a string and found the following code. The code works, but I'm not able to understand the replace part:
function getCharCounts(s) {
var letters = {};
s.replace(/\S/g, function(s){
letters[s] = (isNaN(letters[s] ? 1 : letters[s]) + 1);
});
return letters;
}
console.log(getCharCounts('he111 144pressions'));
Would someone please explain the code to me or write a simpler version?