2017-07-29 58 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