11

我正在尝试使用以下代码计算给定字符串中的单词数:

var t = document.getElementById('MSO_ContentTable').textContent;

if (t == undefined) {
  var total = document.getElementById('MSO_ContentTable').innerText;                
} else {
  var total = document.getElementById('MSO_ContentTable').textContent;        
}
countTotal = cword(total);   

function cword(w) {
  var count = 0;
  var words = w.split(" ");
  for (i = 0; i < words.length; i++) {
    // inner loop -- do the count
    if (words[i] != "") {
      count += 1;
    }
  }

  return (count);
}

在该代码中,我从 div 标签获取数据并将其发送到cword()函数进行计数。虽然 IE 和 Firefox 的返回值不同。正则表达式是否需要任何更改?我表明两个浏览器都发送相同的字符串的一件事是cword()函数内部存在问题。

4

5 回答 5

22

您可以使用splitwordcounter 并将其添加到String原型中:

if (!String.prototype.countWords) {
  String.prototype.countWords = function() {
    return this.length && this.split(/\s+\b/).length || 0;
  };
}

console.log(`'this string has five words'.countWords() => ${
  'this string has five words'.countWords()}`);
console.log(`'this string has five words ... and counting'.countWords() => ${
  'this string has five words ... and counting'.countWords()}`);
console.log(`''.countWords() => ${''.countWords()}`);

于 2011-07-01T05:39:55.010 回答
12

我更喜欢仅使用 RegEx 的解决方案:

var str = "your long string with many words.";
var wordCount = str.match(/(\w+)/g).length;
alert(wordCount); //6

正则表达式是

\w+    between one and unlimited word characters
/g     greedy - don't stop after the first match

括号围绕每场比赛创建一个组。所以所有匹配组的长度应该匹配字数。

于 2014-12-11T08:14:25.573 回答
7

这是我找到的最佳解决方案:

function wordCount(str) { var m = str.match(/[^\s]+/g) return m ? m.length : 0; }

这会反转空格选择,这比\w+它只匹配拉丁字母和 _ 更好(参见http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.2.6

如果您对空格匹配不小心,您将计算空字符串、带有前导和尾随空格的字符串以及所有空格字符串作为匹配项,而此解决方案可以正确处理 , 之类' '的字符串' a\t\t!\r\n#$%() d '(如果您将“正确”定义为 0 和 4)。

于 2016-05-28T00:13:30.733 回答
4

尽管您没有替换任何东西,但您可以巧妙地使用 replace() 方法。

var str = "the very long text you have...";

var counter = 0;

// lets loop through the string and count the words
str.replace(/(\b+)/g,function (a) {
   // for each word found increase the counter value by 1
   counter++;
})

alert(counter);

例如,可以改进正则表达式以排除 html 标签

于 2011-07-01T05:35:45.820 回答
0
//Count words in a string or what appears as words :-)

        function countWordsString(string){

            var counter = 1;

            // Change multiple spaces for one space
            string=string.replace(/[\s]+/gim, ' ');

            // Lets loop through the string and count the words
            string.replace(/(\s+)/g, function (a) {
               // For each word found increase the counter value by 1
               counter++;
            });

            return counter;
        }


        var numberWords = countWordsString(string);
于 2015-05-09T17:25:15.297 回答