Reputation: 761
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.
Upvotes: 0
Views: 75
Reputation: 78520
var blah = "143443143241";
var count = (blah.match(/1/g) || []).length; // <-- returns the number of occurrences. 3 in this case.
Upvotes: 2
Reputation: 32598
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");
Upvotes: 0
Reputation: 34072
"143443143241".match(/1/g).length
Edit: What Joseph said 40 seconds before me.
Upvotes: 1