Javascript 匹配并打印内容+;数量 var str=“每当我假装成鸭子时,我希望鸭子不要对我微笑!”; var matchAntich=['duck','smile','cows'] 对于(var ma=0;ma在“匹配对”数组中搜索匹配项。 >如果为true,则返回

Javascript 匹配并打印内容+;数量 var str=“每当我假装成鸭子时,我希望鸭子不要对我微笑!”; var matchAntich=['duck','smile','cows'] 对于(var ma=0;ma在“匹配对”数组中搜索匹配项。 >如果为true,则返回,javascript,arrays,match,Javascript,Arrays,Match,嗯,我没有主意了,我会解释我需要解决的问题 >在“匹配对”数组中搜索匹配项。 >如果为true,则返回 word=金额(升序) 例如,如果行是“我希望鸭子不要在我假装鸭子时对我微笑!”,则输出应该是: 鸭子=2 微笑=1 (不要打印“cow=0”,这是不带字符的) 但如果这句话是:“今天不是好日子”,就没有产出 谢谢。这将打印出来,而无需按most排序。我马上让它工作:) var string1=“每当我假装成鸭子时,我希望鸭子不要对我微笑!”; var matchAntich=['duck',

嗯,我没有主意了,我会解释我需要解决的问题

>在“匹配对”数组中搜索匹配项。
>如果为true,则返回

word=金额(升序)

例如,如果行是“我希望鸭子不要在我假装鸭子时对我微笑!”,则输出应该是:

鸭子=2
微笑=1

(不要打印“cow=0”,这是不带字符的)

但如果这句话是:“今天不是好日子”,就没有产出


谢谢。

这将打印出来,而无需按most排序。我马上让它工作:)

var string1=“每当我假装成鸭子时,我希望鸭子不要对我微笑!”;
var matchAntich=['duck','smile','cows'];
对于(var ma=0;ma0)
document.write(与[ma]+'='+numMatches+'
'匹配) }
请参见下面代码中的答案。您应该仔细阅读javascript正则表达式对象

var string1 = "I hope ducks don't smile upon me whenever I pretend to be a duck!";

var matchAgainst = ['duck', 'smile', 'cows'];

for (var ma = 0; ma < matchAgainst.length; ma++)
{
    var regexp = new RegExp(matchAgainst[ma], 'g');
    var numMatches = string1.match(regexp).length;
    if (numMatches > 0)
        document.write(matchAgainst[ma] + ' = ' + numMatches + '<br />')
}
var str=“每当我假装成鸭子时,我希望鸭子不要对我微笑!”;
var matchAntich=['duck','smile','cows']
//您需要确保始终使用相同的增量变量值(WASBA++)
对于(var ma=0;ma0)document.write(匹配[ma]+“:“+matches.length+”
”; }
var string1 = "I hope ducks don't smile upon me whenever I pretend to be a duck!";

var matchAgainst = ['duck', 'smile', 'cows'];

for (var ma = 0; ma < matchAgainst.length; ma++)
{
    var regexp = new RegExp(matchAgainst[ma], 'g');
    var numMatches = string1.match(regexp).length;
    if (numMatches > 0)
        document.write(matchAgainst[ma] + ' = ' + numMatches + '<br />')
}
var str = "I hope ducks don't smile upon me whenever I pretend to be a duck!";
var matchAgainst = ['duck', 'smile', 'cows']

//You need to make sure you use the same value for increment variable all the way through (was ba++)
for (var ma = 0; ma < matchAgainst.length; ma++) {
   //Make the search pattern a global regex so it will find all occurences
   var rg = new RegExp(matchAgainst[ma], "g");

   //Run the search, save results to mathces array
   var matches = str.match(rg);

   //If matches found, print the amount
   if(matches.length > 0) document.write(matchAgainst[ma] + ": " + matches.length + "<br>");
}