Javascript:一个字符在字符串中出现多少次?

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

Javascript: How many times a character occurs in a string?

javascriptstring

提问by Ruth

Is there a simple way to check how many times a character appears in a String?

有没有一种简单的方法来检查字符在字符串中出现的次数?

采纳答案by Gumbo

You could remove any other character in the string and check the length:

您可以删除字符串中的任何其他字符并检查长度:

str.replace(/[^a]/g, "").length

Here it is counted how many as are in str.

这里计算as 中有多少个str

回答by Sarfraz

This counts ain below example:

a在下面的例子中很重要:

str = "A man is as good as his word";
alert(str.split('a').length-1);

If you want case insensitive you'd want something like

如果你想要不区分大小写,你会想要类似的东西

alert(str.split( new RegExp( "a", "gi" ) ).length-1);

So that it grabs "A" and "a" ... "g" flag isn't really needed, but you do need the "i" flag

所以它抓住“A”和“a”......“g”标志并不是真正需要的,但你确实需要“i”标志

回答by Drazzah

Use a RegEx to count the number of "a"s in a string.

使用 RegEx 计算字符串中“a”的数量。

var string = 'aajlkjjskdjfAlsj;gkejflksajfjskda';

document.write(string.match(/a/gi).length);

Let me explain how this works:

让我解释一下这是如何工作的:

string.matchThis is a RegEx method. It searches for the specified RegEx inside the specified string (in this case, the string "string").

string.match这是一个正则表达式方法。它在指定的字符串(在本例中为字符串“string”)中搜索指定的 RegEx。

(/a/gi)This is the actual RegEx. It reads, "find the character a." It's very simple. It also carries two flags, the "g" and the "i". The "g" says to find ALL occurences of the character "a". Otherwise it would only find the first one, and it would never count past the number one. The second flag is "i". It makes the RegEx match all cases of that character. If that flag (i) was not there, the code above would only count 4, because it would skip the uppercase "A" in the string. Because of the "i", it will match upper andlower case. Remove the "i" if you you want to match letter case.

(/a/gi)这是实际的正则表达式。上面写着“找到字符 a”。这很简单。它还带有两个标志,“g”和“i”。“g”表示查找字符“a”的所有出现。否则它只会找到第一个,并且永远不会超过第一。第二个标志是“i”。它使 RegEx 匹配该字符的所有情况。如果那个标志 (i) 不存在,上面的代码只会计数 4,因为它会跳过字符串中的大写“A”。因为“i”的,它会上匹配小写。如果要匹配字母大小写,请删除“i”。

string.matchreturns an array of all of the matches, so we use the lengthmethod to retrieve the number of array entries. Simple as that!

string.match返回所有匹配length项的数组,因此我们使用该方法来检索数组条目的数量。就那么简单!

回答by simhumileco

In my opinion it is more convenient and safe to avoid regular expressions in this case

在我看来,在这种情况下避免使用正则表达式更方便和安全

It's because if we want to be able to count any kind of characters then we need to consider two expressions. One for commoncharacters and second for specialcharacters for example like [, ], ^and so on. It's easy to forget about it, but even if we remember it, I think we're unnecessarily expanding our code.

这是因为如果我们想要能够计算任何类型的字符,那么我们需要考虑两个表达式。对于一个普通的字符和第二的特殊例如像人物[]^等等。忘记它很容易,但即使我们记得它,我认为我们也不必要地扩展了我们的代码。

In this casefor string strand character chworks each of these solutions:

这种情况下,字符串str和字符ch适用于这些解决方案中的每一个:

let count = str.split(ch).length - 1

(thanks to @Sarfraz)

(感谢@Sarfraz

or

或者

let count = str.split('').filter(x => x == ch).length

or

或者

let count = 0
str.split('').forEach(x => x == ch ? count++ : null)

Enjoy!

享受!

回答by Darin Dimitrov

var s = "dqsskjhfds";
alert(s.length - s.replace(/a/g, "").length); // number of 'a' in the string

回答by Nitin .

var a = "acvbasbb";
var b= {};
for (let i=0;i<a.length;i++){
    if((a.match(new RegExp(a[i], "g"))).length > 1){
        b[a[i]]=(a.match(new RegExp(a[i], "g"))).length;
    }
}
console.log(b);