【问题标题】:Counting word occurrences in a string javascript [duplicate]计算字符串中的单词出现次数javascript [重复]
【发布时间】:2019-07-31 18:42:30
【问题描述】:

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

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

关于我可能做错了什么的任何指示? 我的目标是重复这个函数,使其显示为thefoxbrown

有什么建议吗? 谢谢你

【问题讨论】:

    标签: javascript arrays string search count


    【解决方案1】:

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

    • /fox/i === arr[i] 永远不会是真的。因为 /fox/i 是正则表达式,而 arr[i] 是原始字符串。
    • count 之前的 return 语句没有任何意义。
    • 您将 theSentence 拆分到函数之外并存储在 arr 中,但在您的函数调用中,您只传递了 theSentence 它应该在的位置 arr 或使用 split inside 函数

    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);

    【讨论】:

    • 我正在节点上对其进行测试,/gi reg 表达式标志未正确呈现,但当我使用“/i”正则表达式时,您的更正有效。你为什么在/g 到reg 表达式?感谢您的帮助!
    • @Squirtle g 是一个全局标志,用于匹配多个匹配项。但在这种情况下不需要它,我的错是我放错了,我已经删除了它
    【解决方案2】:

    有几个问题:

    1. 您正在使用return count++,这将停止函数的进一步执行。应该删除return
    2. 您将RegExp()string 进行比较,而不是使用test() 方法RegExp()
    3. 你应该return console.log(...)它会返回undefined。您可以返回该值,然后将其记录下来。

    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 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));

    更好的方法是在Regex 中使用String.prototype.match()g 标志。如果您想匹配整个单词 fox 使用 /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));

    【讨论】:

    • 最后一个代码错误。只需在末尾添加fox 即可查看结果。我想这是一个错字一个额外的空间
    • 你是对的。有什么修复建议吗?
    • 您可以添加一个替代/fox ?/gi
    • @Code Maniac 感谢它的工作。但我不认为 OP 只想检查整个单词。
    • 如果你使用 match 而不是你需要使用单词边界 (\b) 或者你现在写的那个
    【解决方案3】:

    第二个参数被命名为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); 

    您可以考虑使用该模式

    /\bfox\b/i
    

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

    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);

    【讨论】:

      【解决方案4】:

      您正在传递正则表达式,因此请与 pattern.test(str) 一起使用。我可以用简单格式重写代码

      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= arr.split(' ');
         return arr.filter(a=> word.test(a)).length
      }
      
      console.log(findWord(/fox/i, theSentence));
      console.log(findWord(/The/i, theSentence));

      【讨论】:

      • 有一个错字。我猜你的意思是pattern.test(str) 不是pattern.text(str)
      【解决方案5】:

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

      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;
          count=arr.filter(e=>e.toLowerCase()==word.toLowerCase()).length
          return word +": " + count;
      }
      
      console.log(findWord('the',arr))

      【讨论】:

      • 当预期结果为2时,它会不断返回1
      • 检查编辑
      猜你喜欢
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 2018-10-16
      • 2015-09-14
      • 2012-05-31
      相关资源
      最近更新 更多