计算字符串JavaScript中特定单词的总数 [英] Count Total Amount Of Specific Word In a String JavaScript

查看:63
本文介绍了计算字符串JavaScript中特定单词的总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找出某个特定单词在String JavaScript中出现多少次,或者我们可以说出JavaScript中完整的句子字符串中匹配/匹配单词的总数.

  query ="fake";var inputString ="fakefakefakegg假的00f0 221您好,假冒错误的信息infofddddfakefake,假冒错误的信息cofo假冒,伪造的假冒伪劣产品", 

预期结果= 13(上述句子中有13个假冒商品)

解决方案

这里有两种方法来查找字符串中出现的匹配词的总数.

第一个功能允许您将查询作为输入.第二个使用JavaScript的.match函数.

两种引入的方法对任何炭都有抵抗力,并且独立于分离器和分离器,例如焦炭"."或,".

str1是您的查询

  str1 =假"; 

str2是整个字符串:

  var inputString =" fakefakefakegg假00f0 221您好,wo假misinfofakeddfakefake,Wo 431 ,, asd misinfo misinfo co w fake sosis bandarimikhori ?,假冒伪造的假冒" ;; 

方法1:使用JavaScript的.indexOf或.search函数(可以提供输入的优点)

 函数CountTotalAmountOfSpecificWordInaString(str1,str2){令next = 0;让findedword = 0;做 {var n = str2.indexOf(str1,next);findedword = findedword +1;下一个= n + str1.length;}同时(n> = 0);console.log(总找到的单词:",foundedword-1);返回找到的单词;} 

方法2:使用JavaScript的.match函数:

 /*** @return {number}*您必须在此解决方案中手动将伪造作为查询!!!!坏处*/函数CountTotalAmountOfMachedWordInaString(str2){让machedWord = 0;machedWord = str2.match(/fake/g).length;console.log("total finded mached:",machedWord);返回machedWord;} 

调用函数(输入):

  CountTotalAmountOfSpecificWordInaString("fake","fake fakefakegg fake 00f0 221",您好wo fake rld fakefakefake,lklsak dalkkfakelasd co wo fake,fake fake false false");CountTotalAmountOfMachedWordInaString("sosis bandarie伪造khiyarshour sosis,将bar伪造为sosis3");//功能1输出:总假= 13,功能2输出:总假= 2 

I want to find out how many time a specific words occur in a String JavaScript, or we can say the total amount of matched/match word with the complete sentence string in JavaScript.

query = "fake";

var inputString = "fakefakefakegg fake 00f0 221 Hello wo fake misinfo fakeddfakefake , wo misinfo misinfo co wo fake , fake fake fake ";

expected result = 13 (there is 13 fake in the above sentence)

解决方案

Here are two methods 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");


//Function 1 Output: total Fake = 13 , Function 2 Output: total Fake = 2

这篇关于计算字符串JavaScript中特定单词的总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆