2017-07-26 40 views
3

我正在製作一個javascript程序,它在文本中搜索我的名字並在控制檯中記錄相同的頻率。 我首先檢查文本的每個字母,當它與我的名字的第一個字母相匹配時,我使用另一個for循環將字母推入名爲hits的數組中。字符串「text」中的字母被推送到我的名字的長度推()。 在此之後,我檢查數組「命中」和字符串「myName」是否相等,如果它們相等,我增加一個計數。 但我的代碼不工作,我不知道爲什麼,我非常想到它,但都是徒勞的。請幫忙。javascript,搜索我的名字

var text="abhishek apolo bpple abhishek",myName="abhishek",hits=[]; 
 
var count=0; 
 
for(i=0;i<text.length;i++) 
 
{ 
 
    if(text[i]===myName[0]) 
 
    { 
 
     for(j=i;j<(i+myName.length);j++) 
 
     { 
 
      hits.push(text[j]); 
 
     } 
 
    } 
 
    if(myName==hits) 
 
    { 
 
     hits=[]; 
 
     count=count+1; 
 
    } 
 
    hits=[]; 
 
} 
 
if(count===0) 
 
    console.log("Name not found!"); 
 
else 
 
    console.log(count); 
 
    
 

+1

那麼正則表達式呢? –

+0

'if(myName == hits)'永遠不會通過,因爲您正在比較字符串和數組。 – Teemu

+0

是否有一個原因,你是從頭開始而不是使用像'indexOf'這樣的搜索功能? – Kaddath

回答

4

你的代碼是失敗因爲你正在比較數組與字符串,這會給你錯誤的,你可以使用join()從數組字符串中獲得字符串,或者更好的方法是使用常規的ex PRESSION,像這樣:

var text="abhishek apolo bpple abhishek",myName="abhishek",hits=[]; 
 
var count=0; 
 
console.log((text.match(new RegExp(myName, 'g'))).length);   
 

0

你比較數組的字符串。比較前,使用join()方法將其轉換爲字符串。

例如

if(myName==hits.join('')) 

工作代碼:

var text="abhishek apolo bpple abhishek",myName="abhishek",hits=[]; 
 
var count=0; 
 
for(i=0;i<text.length;i++) 
 
{ 
 
    if(text[i]===myName[0]) 
 
    { 
 
     for(j=i;j<(i+myName.length);j++) 
 
     { 
 
      hits.push(text[j]); 
 
     } 
 
    } 
 
    
 
    if(myName==hits.join('')) 
 
    { 
 
     hits=[]; 
 
     count=count+1; 
 
    } 
 
    hits=[]; 
 
} 
 
if(count===0) 
 
    console.log("Name not found!"); 
 
else 
 
    console.log(count);

爲獲得出現的次數更有效的方式來看看答案在How to count string occurrence in string?

0

只需使用split()做。 myName是字符串hits是陣列中的兩個不相等的,任何情況下

var text = "abhishek apolo bpple abhishek"; 
 
var myName = "abhishek"; 
 

 
var count = text.split(myName).length - 1 
 
if (count === 0) { 
 
    console.log("Name not found!"); 
 
} else { 
 
    console.log(count); 
 
}

1

你可以使用正則表達式來搜索你的名字,這是非常簡單的:

var name = "abhishek apolo bpple abhishek"; 
var regexName = /abhishek/g; 
var matchname = name.match(regexName); 
if(matchname === null) { 
    console.log("Name not found!"); 
} else { 
    console.log(matchname.length); 
} 
0

什麼正則表達式?

var text="abhishek apolo bpple abhishek", 
 
    myName="abhishek", 
 
    hits=[], 
 
    regexp = new RegExp(myName,'g'), 
 
    found ; 
 

 
// Thanks to https://stackoverflow.com/a/6323598/2846837 
 
do { 
 
    found = regexp.exec(text) ; 
 
    if (found) 
 
    hits.push(found[2]) ; 
 
} while (found) ; 
 

 
console.log(myName+' found : '+hits.length) ;

0
var text="abhishek apolo bpple abhishek",myName="abhishek",hits=[]; 

var array_text = text.split(" "); 
var count = 0 
for(var i in array_text){ 
    if(myName == (array_text[i])){ 
    count++; 
    hits.push(array_text[i]); 
    } 
} 
if(count===0) 
    console.log("Name not found!"); 
else 
    console.log(count); 
1

使用Array#reduce一個整潔的方法是:

const text = 'History repeats itself. History repeats itself. Historians repeat each other.' 
 

 
function count(needle, haystack) { 
 
    return haystack 
 
    .split(' ') 
 
    .reduce((c, e) => e === needle ? ++c : c, 0) 
 
} 
 

 
console.log(count('repeats', text)) 
 
console.log(count('repeat', text))
.as-console-wrapper { max-height: 100% !important; top: 0; }

編輯:正如假設顯示此解決方案比正則表達式慢。