Javascript 如何计算随机字符串中的字母数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7349312/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:55:30  来源:igfitidea点击:

How to count the number of letters in a random string?

javascript-eventsjavascript

提问by Mellon

If there is a random string get from SERVER:

如果从 SERVER 获取随机字符串:

var str='I am a student in XXX university, I am interested...'

The strcan contain random number of words with spaces. That's the content of the string is unpredictable.

str可包含有空格的话的随机数。那就是字符串的内容是不可预测的。

In javascript, how to count the number of letters in the string (spacesbetween words are exclusivefrom the counting). For example "I have a car" should be counted 9 letters.

在javascript中,如何计算字符串中的字母数(单词之间的空格计入)。例如“I have a car”应该算9个字母。

回答by Andy E

Assuming you only want alpha characters, get rid of other characters first using replace():

假设您只想要字母字符,请先使用replace()以下方法删除其他字符:

var str='I am a student in XXX university, I am interested...';

alert(str.replace(/[^A-Z]/gi, "").length);

You can add 0-9to the [^A-Z]character class if you want to count numbers as letters. If you only want to remove white-space, change the regular expression to /\s/g

如果您想将数字视为字母,您可以添加0-9[^A-Z]字符类中。如果您只想删除空格,请将正则表达式更改为/\s/g

回答by andlrc

We split every space and join the array again:

我们拆分每个空间并再次加入数组:

var str='I am a student in XXX university, I am interested...'

str = str.split(" ").join("");

alert(str.length);

http://jsfiddle.net/AwVBJ/

http://jsfiddle.net/AwVBJ/

回答by Jamiec

You could coiunt the number of matches using the regex \w- which matches any alphanumeric character or [a-zA-Z]for any alpha character

您可以使用正则表达式计算匹配的数量\w- 匹配任何字母数字字符或[a-zA-Z]任何字母字符

eg:

例如:

var numChars = "I have a car".match(/[a-zA-Z]/g).length;
// numChars = 9

Live example: http://jsfiddle.net/GBvCp/

现场示例:http: //jsfiddle.net/GBvCp/

回答by confucius

var temp = str;

temp= temp.replace(/[^a-zA-Z]+/g,"");

temp.lengthwill give u the number of characters

temp.length会给你字符数

回答by CatchingMonkey

And yet another way:

还有一种方式:

var str = "I have a car";   
while (str.indexOf(' ') > 0) { 
    str = str.replace(' ' , '');
    }
var strLength = str.length;     

回答by mauman

Count the number of spaces (e.g. http://p2p.wrox.com/javascript-how/70527-count-occurrence-character-string.html), then subtract that from the length.

计算空格的数量(例如http://p2p.wrox.com/javascript-how/70527-count-occurrence-character-string.html),然后从长度中减去它。

    var testString = ' my test string has  a number of spaces ';
    alert('Number of spaces:' + (testString .replace(/[^ ]/g, '').length));
    alert('Number of characters:' + (testString.length));
    alert('Number of characters excluding spaces:' + 
        (testString.length - (testString     .replace(/[^ ]/g, '').length)));

Note: it correctly counts double spaces and spaces at the ends.

注意:它正确计算两端的双空格和空格。