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

时间:2020-12-14 23:55:15

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

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

5 个解决方案

#1


48  

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.

在这里,它被计算在str中有多少个as。

#2


26  

This counts a in below example:

下面的例子说明了这一点:

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”标志

#3


11  

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.match This is a RegEx method. It searches for the specified RegEx inside the specified string (in this case, the string "string").

字符串。匹配这是一个RegEx方法。它在指定的字符串中搜索指定的RegEx(在本例中是字符串“string”)。

(/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 and lower case. Remove the "i" if you you want to match letter case.

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

string.match returns an array of all of the matches, so we use the length method to retrieve the number of array entries. Simple as that!

字符串。match返回所有匹配项的数组,因此我们使用length方法检索数组条目的数量。就这么简单!

#4


3  

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

#5


0  

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

#1


48  

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.

在这里,它被计算在str中有多少个as。

#2


26  

This counts a in below example:

下面的例子说明了这一点:

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”标志

#3


11  

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.match This is a RegEx method. It searches for the specified RegEx inside the specified string (in this case, the string "string").

字符串。匹配这是一个RegEx方法。它在指定的字符串中搜索指定的RegEx(在本例中是字符串“string”)。

(/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 and lower case. Remove the "i" if you you want to match letter case.

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

string.match returns an array of all of the matches, so we use the length method to retrieve the number of array entries. Simple as that!

字符串。match返回所有匹配项的数组,因此我们使用length方法检索数组条目的数量。就这么简单!

#4


3  

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

#5


0  

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