Answer by N Djel Okoye for How to count string occurrence in string?
This function will tell you if the substring is in the string and how many times.const wordInText = (wordToFind, wholeText) => { const wordToFindRegex = new RegExp(wordToFind, 'gi'); const...
View ArticleAnswer by Mr. Doge for How to count string occurrence in string?
added this optimization:How to count string occurrence in string?This is probably the fastest implementation here, but it would be even faster if you replaced "++pos" with "pos+=searchFor.length"...
View ArticleAnswer by Denise Ignatova for How to count string occurrence in string?
Here is my solution. I hope it would help someoneconst countOccurence = (string, char) => {const chars = string.match(new RegExp(char, 'g')).lengthreturn chars;}
View ArticleAnswer by Elell for How to count string occurrence in string?
Here is my solution, in 2022, using map() and filter() :string = "Xanthous: A person with yellow hair. Her hair was very xanthous in colour." count = string.split('').map((e,i) => { if(e === 'e')...
View ArticleAnswer by Balaji Sukumaran for How to count string occurrence in string?
We can use the js split function, and it's length minus 1 will be the number of occurrences.var temp = "This is a string.";alert(temp.split('is').length-1);
View ArticleAnswer by polendina for How to count string occurrence in string?
This function works in three modes: looking for the frequency of a single character within a string , a contiguous substring within a string then if it does match one it moves right ahead to next one...
View ArticleAnswer by Bhojak Rahul for How to count string occurrence in string?
const getLetterMatchCount = (guessedWord, secretWord) => { const secretLetters = secretWord.split(''); const guessedLetterSet = new Set(guessedWord); return secretLetters.filter(letter =>...
View ArticleAnswer by Sagar Shinde for How to count string occurrence in string?
var mystring = 'This is the lorel ipsum text';var mycharArray = mystring.split('');var opArr = [];for(let i=0;i<mycharArray.length;i++){if(mycharArray[i]=='i'){//match the character you want to...
View ArticleAnswer by H S W for How to count string occurrence in string?
A simple way would be to split the string on the required word, the word for which we want to calculate the number of occurences, and subtract 1 from the number of parts:function...
View ArticleAnswer by Force Bolt for How to count string occurrence in string?
//Try this codeconst countSubStr = (str, search) => { let arrStr = str.split(''); let i = 0, count = 0; while(i < arrStr.length){ let subStr = i + search.length + 1 <= arrStr.length ?...
View ArticleAnswer by Michel for How to count string occurrence in string?
Iterate less the second time (just when first letter of substring matches) but still uses 2 for loops: function findSubstringOccurrences(str, word) { let occurrences = 0; for(let i=0; i<str.length;...
View ArticleAnswer by dimButTries for How to count string occurrence in string?
ES2020 offers a new MatchAll which might be of use in this particular context.Here we create a new RegExp, please ensure you pass 'g' into the function.Convert the result using Array.from and count the...
View ArticleAnswer by Samruddh Shah for How to count string occurrence in string?
var str = 'stackoverflow';var arr = Array.from(str);console.log(arr);for (let a = 0; a <= arr.length; a++) { var temp = arr[a]; var c = 0; for (let b = 0; b <= arr.length; b++) { if (temp ===...
View ArticleAnswer by b_rop for How to count string occurrence in string?
The parameters:ustring: the superset stringcountChar: the substringA function to count substring occurrence in JavaScript:function subStringCount(ustring, countChar){ var correspCount = 0; var corresp...
View ArticleAnswer by fahrenheit317 for How to count string occurrence in string?
You could try thislet count = s.length - s.replace(/is/g, "").length;
View ArticleAnswer by mendezcode for How to count string occurrence in string?
function substrCount( str, x ) { let count = -1, pos = 0; do { pos = str.indexOf( x, pos ) + 1; count++; } while( pos > 0 ); return count; }
View ArticleAnswer by Clean Code Studio for How to count string occurrence in string?
substr_count translated to Javascript from phpLocutus (Package that translates Php to JS)substr_count (official page, code copied below)function substr_count (haystack, needle, offset, length) { //...
View ArticleAnswer by Ashok R for How to count string occurrence in string?
came across this post.let str = 'As sly as a fox, as strong as an ox';let target = 'as'; // let's look for itlet pos = 0;while (true) { let foundPos = str.indexOf(target, pos); if (foundPos == -1)...
View ArticleAnswer by BaseZen for How to count string occurrence in string?
No one will ever see this, but it's good to bring back recursion and arrow functions once in a while (pun gloriously intended)String.prototype.occurrencesOf = function(s, i) { return (n => (n ===...
View ArticleAnswer by Kamal for How to count string occurrence in string?
var countInstances = function(body, target) { var globalcounter = 0; var concatstring = ''; for(var i=0,j=target.length;i<body.length;i++){ concatstring = body.substring(i-1,j); if(concatstring ===...
View Article