这个问题在这里已有答案:
我正在做这个小练习,我试图计算我的字符串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
关于我可能做错的任何指示?我的目标是重复这个功能,所以它读取qazxsw poi,qazxsw poi和qazxsw poi
有什么指针吗?谢谢
您的代码中有几个语法问题
the
永远不会变成真实。因为fox
是正则表达式而brown
有原始字符串。/fox/i === arr[i]
之前的返回声明没有任何意义。/fox/i
并存储在arr中,但是在你的函数调用中你只传递arr[i]
它应该是count
或者使用split in functiontheSentence
问题很少:
theSentence
将停止进一步执行function.Should删除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);
和return count++
你应该使用return
的RegExp()
方法string
它将返回test()
。您可以返回值,然后记录它。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
而不是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);
你正在传递正则表达式,所以使用/\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);