使用 JavaScript 计算字符串中的单词数

问题描述 投票:0回答:5

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

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()
函数内部存在问题。

javascript internet-explorer-9 firefox3.5
5个回答
22
投票

[编辑 2022,基于评论] 如今,人们不会以这种方式扩展原生原型。扩展本机原型而不存在命名冲突危险的一种方法是使用 es20xx

symbol
这是一个使用它的单词计数器的示例

旧答案:您可以使用

split
并将单词计数器添加到
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()}`);

[编辑2024/07]

这是“象征性”扩展

String.prototype
的示例(另请参阅):

const nWords = Symbol(`countWords`);
const letterCount = Symbol(`letterCount`);

// extend String.prototype with symbols
Object.defineProperty(
  String.prototype, 
  nWords, { 
    get() { return this.match(/\w+/g).length; } 
  } );
Object.defineProperty(
  String.prototype, 
  letterCount, { 
    get() { 
      return function(letter, caseSensitive = false) { 
        const mods = `g${caseSensitive ? "" : "i"}`; 
        return this.match(RegExp(letter, mods))?.length ?? 0; 
      } 
    } 
  } );

const testString = document.querySelector(`code`).textContent;

console.log(`[testString] has ${testString[nWords]} words, contains ${
  testString[letterCount](`D`, true)} "D" and ${
    testString[letterCount](`d`)} "d or D"`);
<h3>testString</h3>
<code>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</code>


14
投票

我更喜欢仅正则表达式的解决方案:

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

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


11
投票

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


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)。


3
投票

尽管你没有替换任何东西,但你可以巧妙地使用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 标签,例如


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);
© www.soinside.com 2019 - 2024. All rights reserved.