I've written a small function to count the amount of occurrences of a character within a string. It's been working just fine.
Until i tried to count dots, it keeps giving me half the number it should. What am i doing wrong? Am i not escaping the dots in the right manner?
function count(s1, letter) {
return (s1.length - s1.replace(new RegExp(letter, "g"), '').length) / letter.length;
}
var loc = 'http://www.domain.com/page' // I'm actually using window.location.href in practice.
var someStringWithDots = 'Yes. I want. to. place a. lot of. dots.';
var somestring = 'abbbcdefg';
count(somestring, 'b');
//returns 3 - correct
count(someStringWithDots, '\\.');
//returns 3 - incorrect
count(loc, '\\.');
//returns 1 - incorrect