How to find character appearence in text
How can i find with jquery how many times a character开发者_开发百科 appear in text, for example i have the character 1 and the text 143443143241 so here the character 1 appear 3 times, how can i check that.
var blah = "143443143241";
var count = (blah.match(/1/g) || []).length; // <-- returns the number of occurrences. 3 in this case.
"143443143241".match(/1/g).length
Edit: What Joseph said 40 seconds before me.
This is just a javascript question, or a generic programming question.
function countOccurences(string, testChar) {
var count = 0;
for(var i = 0; i<string.length; i++){
if(string[i] == testChar) { count++; }
}
return count;
}
countOccurences("143443143241", "1");
精彩评论