2017-07-29 60 views
1

我正在嘗試構建一個函數來計算短語中單詞的出現次數。計算單詞出現次數,允許特殊字符和換行符

該功能應該包括短語中的單詞具有附加的非字母字符和/或行尾字符的情況。

function countWordInText(word,phrase){ 
    var c=0; 
    phrase = phrase.concat(" "); 
    regex = (word,/\W/g); 
    var fChar = phrase.indexOf(word); 
    var subPhrase = phrase.slice(fChar); 

    while (regex.test(subPhrase)){ 
     c += 1; 
     subPhrase = subPhrase.slice((fChar+word.length)); 
     fChar = subPhrase.indexOf(word); 
    } 
    return c; 
} 

的問題是,對於一個簡單的值,如

phrase = "hi hi hi all hi. hi"; 
word = "hi" 
// OR 
word = "hi all"; 

返回假值。

回答

1

你寫的算法顯示你花了一些時間試圖讓這個工作。但是,仍然有不少地方不適用。例如,(word,/W/g)實際上並不是創建您可能認爲的正則表達式。

還有一個更簡單的方法:

function countWordInText (word, phrase) { 
    // Escape any characters in `word` that may have a special meaning 
    // in regular expressions. 
    // Taken from https://stackoverflow.com/a/6969486/4220785 
    word = word.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&') 

    // Replace any whitespace in `word` with `\s`, which matches any 
    // whitespace character, including line breaks. 
    word = word.replace(/\s+/g, '\\s') 

    // Create a regex with our `word` that will match it as long as it 
    // is surrounded by a word boundary (`\b`). A word boundary is any 
    // character that isn't part of a word, like whitespace or 
    // punctuation. 
    var regex = new RegExp('\\b' + word + '\\b', 'g') 

    // Get all of the matches for `phrase` using our new regex. 
    var matches = phrase.match(regex) 

    // If some matches were found, return how many. Otherwise, return 0. 
    return matches ? matches.length : 0 
} 

countWordInText('hi', 'hi hi hi all hi. hi') // 5 

countWordInText('hi all', 'hi hi hi all hi. hi') // 1 

countWordInText('hi all', 'hi hi hi\nall hi. hi') // 1 

countWordInText('hi all', 'hi hi hi\nalligator hi. hi') // 0 

countWordInText('hi', 'hi himalayas') // 1 

我把意見貫穿例子。希望這可以幫助你開始!

這裏有一些偉大的地方,瞭解正則表達式的Javascript:

您還可以測試你的正則表達式住Regexr

+0

我能說什麼男人?我昨天掙扎了好幾個小時。我不完全瞭解代碼,但推薦將是一個很大的幫助!非常感謝! – sale108