计算字符串中的单词出现次数javascript [复制]

问题描述 投票:2回答:5

这个问题在这里已有答案:

我正在做这个小练习,我试图计算我的字符串wordtheSentence出现的数量。

const theSentence = "The quick brown fox jumps over the lazy brown dog, who thinks the world is flat and the sky\'s blue. The brown fox then goes to sleep";
let arr= theSentence.split(' ');

function findWord(word, arr){
    let count = 0;
    for(let i = 0; i < arr.length; i++){
        if(arr[i] === word){
            return count++;
        }
    }
    return console.log(word +": " + count);
}

我的功能如下

findWord(/fox/i, theSentence); 

我的预期产量应该是fox: 2

但我的实际出局是/fox/i: 0

关于我可能做错的任何指示?我的目标是重复这个功能,所以它读取qazxsw poi,qazxsw poi和qazxsw poi

有什么指针吗?谢谢

javascript arrays string search count
5个回答
2
投票

您的代码中有几个语法问题

  • the永远不会变成真实。因为fox是正则表达式而brown有原始字符串。
  • /fox/i === arr[i]之前的返回声明没有任何意义。
  • 你在功能之外拆分/fox/i并存储在arr中,但是在你的函数调用中你只传递arr[i]它应该是count或者使用split in function

theSentence

2
投票

问题很少:

  1. 您正在使用theSentence将停止进一步执行function.Should删除arr
  2. 你正在比较const theSentence = "The quick brown fox jumps over the lazy brown dog, who thinks the world is flat and the sky\'s blue. The brown fox then goes to sleep"; function findWord(word, arr){ arr= theSentence.split(' '); let count = 0; for(let i = 0; i < arr.length; i++){ if(word.test(arr[i])){ count++; } } return console.log(word +": " + count); } findWord(/fox/i, theSentence);return count++你应该使用returnRegExp()方法
  3. 你应该string它将返回test()。您可以返回值,然后记录它。

RegExp()

更好的方法是在return console.log(...)中使用undefinedconst theSentence = "The quick brown fox jumps over the lazy brown dog, who thinks the world is flat and the sky\'s blue. The brown fox then goes to sleep fox"; let arr= theSentence.split(' '); function findWord(word, arr){ let count = 0; for(let i = 0; i < arr.length; i++){ if(word.test(arr[i])){ count++; } } return word +": " + count } console.log(findWord(/fox/i, arr));标志。如果你想匹配整个单词String.prototype.match()使用g作为正则表达式。如果你想从里面的单词匹配使用Regex

fox

1
投票

第二个参数名为/fox ?/gi,听起来像是要传递数组 - 如果这是你想要的,请传递/fox/gi而不是const theSentence = "The quick brown fox jumps over the lazy brown dog, who thinks the world is flat and the sky\'s blue. The brown fox then goes to sleep"; function findWord(word, str){ return word + ": " + str.match(word).length } console.log(findWord(/fox/gi, theSentence));

arr是一个正则表达式,它是一个对象,而不是一个原始 - 它永远不会是arr到其他任何东西。在正则表达式中对数组中的每个单词调用theSentence,并使用/fox/i增加===,并且不在循环内返回 - 如果你.test,函数将立即终止。 (你希望它迭代数组中的所有单词)

count

您可以考虑使用该模式

count++

相反 - 这将匹配return在输入中存在的次数,不区分大小写,每边都有一个字边界。

const theSentence = "The quick brown fox jumps over the lazy brown dog, who thinks the world is flat and the sky\'s blue. The brown fox then goes to sleep";
let arr= theSentence.split(' ');

function findWord(word, arr){
    let count = 0;
    for(let i = 0; i < arr.length; i++){
        if(word.test(arr[i])){
            count++;
        }
    }
    return console.log(word +": " + count);
}
findWord(/fox/i, arr); 

1
投票

你正在传递正则表达式,所以使用/\bfox\b/i 。我可以用简单的格式重写代码

fox

1
投票

试试这个。使用过滤器搜索数组中的单词并返回计数

const theSentence = "The quick brown fox jumps over the lazy brown dog, who thinks the world is flat and the sky\'s blue. The brown fox then goes to sleep";

function findWord(pattern, sentence){
  const count = sentence.match(pattern).length;
  return console.log(pattern +": " + count);
}
findWord(/\bfox\b/gi, theSentence);
© www.soinside.com 2019 - 2024. All rights reserved.