使用 JavaScript 计算字符串中的单词数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6543917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count number of words in string using JavaScript
提问by V_B
I am trying to count the number of words in a given string using the following code:
我正在尝试使用以下代码计算给定字符串中的单词数:
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);
}
In that code I am getting data from a div tag and sending it to the cword()
function for counting. Though the return value is different in IE and Firefox. Is there any change required in the regular expression? One thing that I show that both browser send same string there is a problem inside the cword()
function.
在该代码中,我从 div 标签获取数据并将其发送到cword()
函数进行计数。虽然返回值在 IE 和 Firefox 中是不同的。正则表达式是否需要任何更改?我表明两个浏览器都发送相同字符串的一件事是cword()
函数内部存在问题。
采纳答案by Ibu
You can make a clever use of the replace() method although you are not replacing anything.
尽管您没有替换任何内容,但您可以巧妙地使用 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);
the regex can be improved to exclude html tags for example
例如,可以改进正则表达式以排除 html 标签
回答by KooiInc
You can use split
and add a wordcounter to the String
prototype:
您可以使用split
wordcounter 并将其添加到String
原型中:
String.prototype.countWords = function(){
return this.split(/\s+/).length;
}
'this string has five words'.countWords(); //=> 5
If you want to exclude things like ... or - in a sentence:
如果你想在一个句子中排除诸如 ... 或 - 之类的东西:
String.prototype.countWords = function(){
return this.split(/\s+\b/).length;
}
'this string has seven ... words - and counting'.countWords(); //=> 7
回答by DanielH
I would prefer a RegEx only solution:
我更喜欢只有 RegEx 的解决方案:
var str = "your long string with many words.";
var wordCount = str.match(/(\w+)/g).length;
alert(wordCount); //6
The regex is
正则表达式是
\w+ between one and unlimited word characters
/g greedy - don't stop after the first match
The brackets create a group around every match. So the length of all matched groups should match the word count.
括号围绕每场比赛创建一个组。所以所有匹配组的长度应该匹配字数。
回答by aaron
This is the best solution I've found:
这是我找到的最佳解决方案:
function wordCount(str) {
var m = str.match(/[^\s]+/g)
return m ? m.length : 0;
}
function wordCount(str) {
var m = str.match(/[^\s]+/g)
return m ? m.length : 0;
}
This inverts whitespace selection, which is better than \w+
because it only matches the latin alphabet and _ (see http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.2.6)
这会反转空白选择,这比\w+
因为它只匹配拉丁字母和 _更好(参见http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.2.6)
If you're not careful with whitespace matching you'll count empty strings, strings with leading and trailing whitespace, and all whitespace strings as matches while this solution handles strings like ' '
, ' a\t\t!\r\n#$%() d '
correctly (if you define 'correct' as 0 and 4).
如果您对空格匹配不小心,您将计算空字符串、带有前导和尾随空格的字符串以及所有空格字符串作为匹配项,而此解决方案正确处理类似' '
, 的字符串' a\t\t!\r\n#$%() d '
(如果您将“正确”定义为 0 和 4)。
回答by Roman
//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);