2

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

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

任何指针?谢谢你

4

5 回答 5

2

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

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

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

于 2019-03-10T06:42:17.410 回答
2

有几个问题:

  1. 您正在使用return count++它将停止功能的进一步执行。应该删除return
  2. 您正在与RegExp()a进行比较,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));

String.prototype.match()一个更好的方法是gRegex. 如果要匹配整个单词,请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));

于 2019-03-10T06:49:55.100 回答
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";
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))

于 2019-03-10T06:41:07.733 回答
1

第二个参数名为arr,这听起来像是您要传递一个数组 - 如果这是您想要的,请传递arr而不是 theSentence

/fox/i是一个正则表达式,它是一个对象,而不是一个原始的 - 它永远不会是===其他任何东西。调用.test数组中的每个单词,在正则表达式上,并以 递增countcount++并且不要在循环内返回- 如果是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);

于 2019-03-10T06:41:49.763 回答
1

你正在传递正则表达式,所以与pattern.test(str).And 我可以用简单的格式重写代码

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

于 2019-03-10T06:49:12.990 回答