Count Total Amount Of Specific Word In a String JavaScript

function count(str, find) {
    return (str.split(find)).length - 1;
}

count("Good", "o"); // 2

0
3
Robin Manoli 100 points

                                    var input = document.getElementById('typed-text');

input.onkeydown = function (e) {

    if (e.keyCode === 13) {

        var paragraph = document.getElementById('paragraph');
        var result = document.querySelector('.result-output');
        var regexp = new RegExp(this.value, 'g');
        var textIncludes = paragraph.textContent.match(regexp);
            
        if (result)
            result.remove();

        paragraph.innerHTML = paragraph.textContent.replace(
            regexp,
            '<span style="color:red">' + this.value + '</span>');

        paragraph.insertAdjacentHTML(
            'afterend',
            '<span class="result-output" style="display: block; padding: 5px; margin-top: 10px; background: #eee; color: green;">' + (textIncludes ? textIncludes.length : 0) + ' words has been found.</span>');
            
    }

}
<div id="highlights">
        <div class="container">
            <div class="row">
                <div class="col-md-12" id="paragraph">
                    <p>
                       text
                    </p>
                </div>
                <div class="col-md-12 input-group mt-3">
                    <div class="input-group-prepend">
                        <span class="input-group-text" id="basic-addon1">
                            <i class="fas fa-pencil-alt"></i>
                        </span>
                    </div>
                    <input id="typed-text" type="text" class="form-control" placeholder="Type text">
                </div>
            </div>
        </div>
    </div>

0
0
0
4
Van Minh 130 points

                                    Here are two methods (Scroll down please here!) to find the total number of occurrence match
words in the string.

The first function allows you to give a query as input.
The second one uses the .match function of JavaScript.

Both introduced methods are resistant for any chars and 
independent of splitter and separator like " " or ",".

str1 is your query

str1 = "fake";  

str2 is the whole string:

var inputString = "fakefakefakegg fake 00f0 221 Hello wo fake misinfo
fakeddfakefake , wo  431,,asd misinfo misinfo co wo fake sosis bandari
mikhori?, fake fake fake ";

Method 1 : use .indexOf or .search function of JavaScript 
(advantage you can give input)


function CountTotalAmountOfSpecificWordInaString(str1, str2)
{
    let next = 0;
    let findedword = 0;
        do {
            var n = str2.indexOf(str1, next);
            findedword = findedword +1;
            next = n + str1.length;
            }while (n>=0);
     console.log("total finded word :" , findedword - 1 );
     return findedword;
   }
Method 2 : use .match function of JavaScript:



/**
 * @return {number}
 *  you have to put fake as query manually in this solution!!! disadvantage
 */

function CountTotalAmountOfMachedWordInaString(str2) {
    let machedWord = 0;
    machedWord = str2.match(/fake/g).length; 
    console.log("total finded mached :" , machedWord);
    return machedWord;
}
call the functions (Inputs):

CountTotalAmountOfSpecificWordInaString("fake" , "fake fakefakegg fake 00f0 221 Hello wo fake rld fakefakefake , wo lklsak dalkkfakelasd co wo fake , fake fake fake" );
CountTotalAmountOfMachedWordInaString("sosis bandarie fake  khiyarshour sosis , droud bar fake to sosis3");

0
0
3.86
7

                                    function WordCounter (str) {
	var words = str.split(" ").length;
	return words;
}
// this should work!

3.86 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to count sentecs word using javascript js count word count the number of words in a string javascript count word in javascript count word formula javascript javascript how to count number of words in a string word count and show .. in javascript word count in js program in javascript to count the total number of words in a string. count the number of occurrences of each character in a string in javascript javascript word count javascript count occurrences of word in string javascript count number of character occurrences in string how to count characters occurrence in word in javascript js count how many specific words are in a string how to count a specific word in javascript count total number of words in a string js javascript count number of times word appears in string count how many times a word appears in string javascript javascript count the number of occurrences in a string count specific word in string javascript count occurrences of word in string javascript javascript count word occurrences in string javascript count how many times a word appears in a string count number of word in string js javascript count number of occurrences in string word count javascript count the occurrence of a word in a string in javascript count word js how to find total number of words in a string using avascript how to check length of string in if statement javascript how to count number of words in a string javascript js word count string get length of string angular how to count words in a string in javascript javascript get message length can you .length a string js count of 1 in string javascript create a function to count the words in a string javascript javascript section length count how many times a word appears in a string javascript get string character length js string length not count n javascript get legnth of string word count fuormula javscript get length of a word check how many word javascript conditional on legth of string angular html javascript find number of words in string text number count javascript number of characters string javascript how to check for multiple occurrences of a string in javascript how to display word length in javascript how to check length of words in js how to check lengthof words in js count words in a string not including numbers javascript count words in a string no numbers javascript get length of string how to find the lenght in the js create word count function in javascript javascript count words in string check the length of the string count number of words javascript count word in string js how to check how word in a string java script how to get size of a string in javascript javascript string count words find number of a specific words in a string js find number of words in a string js react check word cont in string word counter js js test string length string.count js javascript word count function how to count words in string in javascript how to count words javascript typescript count words in string count number of words in a sentence javascript javascript amount of words in string Count Total Amount Of Specific Word In a String JavaScript find a count of word with the pattern using javascript find a number of word with the pattern using javascript count a word with the pattern using javascript count string in string js find total number of words in string javascript how to count the specific words in string javascript how to find and count the specific words in string javascript how to count the specific words in string javascript count words in js how to set string length in javadcript counting words javascript counting words in js return word count js how to ger perticular length of string from big string in javascript count word include in string javascript count word in string javascript count word in string javasc how to get word count of string javascript counting words in paragraph js string length returns word count javascript js check size of string javascript calculate words of text string check length of string string.count in js showing a string to a certain number of words in javascript showing specific number word from a string in javascript count word in input javascript js get amount of words in string count word javascript how to check the lenght of a text in js check ( count in text js word count using javascript count words javascript words count js count thee numeer of words in a strin g In JS get number of words in string javascript how to count characters in word in javascript a string get word count javascript word counter javascript return number of words in a string javascript string.length how to know the length of string js function which return the length of a string javascript count how many words in sentence javascript check number of words in string javascript how many words are in a sentence javascript check caracther length string js all words in string javascript how many words javascript javascript count words in sentence js function that returns number of characters in a string value match() to count the number of word in a sentence javascript match() to count the number of word in a sentence javascript Count the number of word yes in this sentence javascript how to count number of words in javascript how many words in a string solution javascript how many words in a string solution in javascript how many words in a string javascript solution js if lenght find length of string angular how to find number of words in a string in javascript javscript count words in string return the number of characters in a string using if statement javaScript print count words javascript compare string length in javascript how to count each word in string in javascript check size of string javascript javascript string count angular string character length string length javascript how many words in a string js count string count words typescript count words js how to words from a string in javascript how to return the number of characters in javascript how to figure out what word did someone meant javascript count words in javascript how to get the number of characters in a string in javascript get length of string javascript and compare login page with user setting length of variable javascript js length write javascript number characters in string angular Returns the length of a String object string lengthdefined javascript strlen is not defined javascript how to count character length in javascript get character count in word js count words in a string javascript js number of words in string find number of words in a string javascript js how many words in string how to count the number of words in a string in javascript javascript count words in a string js find word by length javascript number of words in string separate words in a string with space lodash word count in string javascript check length of an item in a string of words javascript js count words in string word count js count words in paragraph js javascript count number of words in a string how to find word count of a key in javascript count the number of words in an string, javascript count the number of words without the html tags, vuejs count words in string js javascript word count function to find occurrence of word how to find word more than 5 character in js javascript check word length in a string word count in javascript length of words in javascript select number of words in javascript how to give string size in javascript count words in a sentence javascript js length between js count words check string length js check velue string length count number of words in a string except word checking length of a string javascript js how to get the lengh of a string get string length javascript counting characters in a string javascript how to tell how long a string word character length in javascript find the length of a word in javascript function to get string length in js how to get string length length method js words of string javascript how to get size of string in js how to count words in javascript GETTING LENGTH OF A VARIABLE JAVASCRIPT getting length of string word and charachetr count javascript javascript to count words string.count javascript html count how a specific word is in a string html count how many words contains in stirng check length of string javascript javascript count string make a typescript program that counts number of words in text file javascript count number of words in string js count specefic words in text js count words in text .length in if js check lenght of string increase character count javascript string characters length use of length javascript count words in largs string javascript number of words in string javascript string count javascript check length string count the string word and replace with it javascript replace every word in string s by its length of word javascript html how to count number of words letter length js get length of var javascript no length check js return only amount of words count words in string javascript length of string how to get string count in js how to find number of characters in a string in javascript count number of words in a string javascript javscript get character size how many words in a string javascript how to count words in a string javascript how length works in javascript count words characters in array javascript count word lenght in array of strings js how to count words in a sentence javasript though html input count words with javascript compare string length javascript js count how many a charcter is in string javascript string word count return length of string javascript get length of string in javascript js string.length count string javascript how to check how many strings are in a sentence javascript get word count in js js word count how to check javascript word limit js how to count the number of words javascript search word count javascript Document word search and count
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source