我正在做这个小练习,试图计算字符串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
关于我可能做错了什么的任何指示?
我的目标是重复此功能,以使其读取the
,fox
和brown
有指针吗? 谢谢
答案 0 :(得分:2)
您的代码中有几个语法问题
/fox/i === arr[i]
永远不会是真的。因为 /fox/i
是正则表达式,而 arr[i]
具有原始字符串。count
之前的return语句没有任何意义。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);
答案 1 :(得分:2)
有几个问题:
return count++
,它将停止功能的进一步执行。应删除return
RegExp()
与string
进行比较,而应该使用test()
的{{1}}方法RegExp()
会返回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()
和g
标志。如果要匹配整个单词Regex
,请使用fox
作为正则表达式。如果要从单词内部进行匹配,请使用/fox ?/gi
/fox/gi
答案 2 :(得分: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))
答案 3 :(得分:1)
第二个参数名为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 :(得分:1)
您正在传递正则表达式,因此可以与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));