javascript - 计算单词出现的次数,允许特殊字符和换行符

标签 javascript node.js regex count find-occurrences

我正在尝试构建一个函数来计算某个词在短语中出现的次数。

该函数应包括短语中的单词具有额外的非字母字符和/或行尾字符的情况。

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";

它返回错误值。

最佳答案

您编写的算法显示您花了一些时间来尝试让它发挥作用。但是,仍然有很多地方不起作用。例如,(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 测试您的正则表达式。 .

关于javascript - 计算单词出现的次数,允许特殊字符和换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45384051/

相关文章:

javascript - 如何从 URL 中的哈希中获取搜索查询

linux - 如何通过 linux apache 服务器在浏览器上运行 node.js?

python - 将自定义格式数据加载到 splunk 中

javascript - 聚焦 asp.net 文本框的事件

javascript - Javascript的reduce()函数有什么优点? (和 map ())

Javascript - 获取两个数组之间的对应对象

php - 如何使 AJAX 使用 POST 而不是 GET

javascript - HTML 分页问题

mysql - 对不存在的记录进行序列化响应

c# - 正则表达式未找到所有匹配项 C#