Answer by PhilMaGeo for How to count string occurrence in string?
Answer for Leandro Batista :just a problem with the regex expression."use strict"; var dataFromDB = "testal"; $('input[name="tbInput"]').on("change",function(){var charToTest = $(this).val();var...
View ArticleAnswer by Fad Seck for How to count string occurrence in string?
String.prototype.Count = function (find) { return this.split(find).length - 1;}console.log("This is a string.".Count("is"));This will return 2.
View ArticleAnswer by Jorge Alberto for How to count string occurrence in string?
Simple version without regex:var temp = "This is a string.";var count = (temp.split('is').length - 1);alert(count);
View ArticleAnswer by Tushar Shukla for How to count string occurrence in string?
Now this is a very old thread i've come across but as many have pushed their answer's, here is mine in a hope to help someone with this simple code.var search_value = "This is a dummy sentence!";var...
View ArticleAnswer by Ayo I for How to count string occurrence in string?
Building upon @Vittim.us answer above. I like the control his method gives me, making it easy to extend, but I needed to add case insensitivity and limit matches to whole words with support for...
View ArticleAnswer by Sunil Garg for How to count string occurrence in string?
var temp = "This is a string.";console.log((temp.match(new RegExp("is", "g")) || []).length);
View ArticleAnswer by bcherny for How to count string occurrence in string?
For anyone that finds this thread in the future, note that the accepted answer will not always return the correct value if you generalize it, since it will choke on regex operators like $ and .. Here's...
View ArticleAnswer by Ranju for How to count string occurrence in string?
var myString = "This is a string."; var foundAtPosition = 0; var Count = 0; while (foundAtPosition != -1) { foundAtPosition = myString.indexOf("is",foundAtPosition); if (foundAtPosition != -1) {...
View ArticleAnswer by Faraz Kelhini for How to count string occurrence in string?
The non-regex version: var string = 'This is a string', searchFor = 'is', count = 0, pos = string.indexOf(searchFor);while (pos > -1) {++count; pos = string.indexOf(searchFor,...
View ArticleAnswer by Meghendra S Yadav for How to count string occurrence in string?
Try it<?php $str = "33,33,56,89,56,56";echo substr_count($str, '56');?><script type="text/javascript">var temp = "33,33,56,89,56,56";var count = temp.match(/56/g);...
View ArticleAnswer by Ismael Miguel for How to count string occurrence in string?
Here is the fastest function!Why is it faster?Doesn't check char by char (with 1 exception)Uses a while and increments 1 var (the char count var) vs. a for loop checking the length and incrementing 2...
View ArticleAnswer by Gere for How to count string occurrence in string?
My solution:var temp = "This is a string.";function countOccurrences(str, value) { var regExp = new RegExp(value, "gi"); return (str.match(regExp) || []).length;}console.log(countOccurrences(temp, 'is'));
View ArticleAnswer by Jason Larke for How to count string occurrence in string?
Super duper old, but I needed to do something like this today and only thought to check SO afterwards. Works pretty fast for me.String.prototype.count = function(substr,start,overlap) { overlap =...
View ArticleAnswer by Simm for How to count string occurrence in string?
I think the purpose for regex is much different from indexOf.indexOf simply find the occurance of a certain string while in regex you can use wildcards like [A-Z] which means it will find any capital...
View ArticleAnswer by Freezy Ize for How to count string occurrence in string?
You can try this:var theString = "This is a string.";console.log(theString.split("is").length - 1);
View ArticleAnswer by Diogo Arenhart for How to count string occurrence in string?
Try this:function countString(str, search){ var count=0; var index=str.indexOf(search); while(index!=-1){ count++; index=str.indexOf(search,index+1); } return count;}
View ArticleAnswer by Vitim.us for How to count string occurrence in string?
/** Function that count occurrences of a substring in a string; * @param {String} string The string * @param {String} subString The sub string to search for * @param {Boolean} [allowOverlapping]...
View ArticleAnswer by Tomas for How to count string occurrence in string?
Just code-golfing Rebecca Chernoff's solution :-)alert(("This is a string.".match(/is/g) || []).length);
View ArticleAnswer by Gumbo for How to count string occurrence in string?
You can use match to define such function:String.prototype.count = function(search) { var m = this.match(new RegExp(search.toString().replace(/(?=[.\\+*?[^\]$(){}\|])/g, "\\"), "g")); return m ?...
View ArticleAnswer by Brandon Frohbieter for How to count string occurrence in string?
function countInstances(string, word) { return string.split(word).length - 1;}console.log(countInstances("This is a string", "is"))
View Article