Answers for "Count Total Amount Of Specific Word In a String JavaScript"

5

javascript count occurrences of letter in string

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

count("Good", "o"); // 2
Posted by: Guest on May-31-2021
0

how to check how many strings are in a sentence javascript

function WordCounter (str) {
	var words = str.split(" ").length;
	return words;
}
// this should work!
Posted by: Guest on June-02-2020
1

javascript Document word search and count

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>
Posted by: Guest on March-14-2020
-1

Count Total Amount Of Specific Word In a String JavaScript

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");
Posted by: Guest on November-27-2020

Code answers related to "Count Total Amount Of Specific Word In a String JavaScript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language