JavaScript:字符在字符串中出现多少次? [英] JavaScript: How many times a character occurs in a string?
本文介绍了JavaScript:字符在字符串中出现多少次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种简单的方法可以检查字符在字符串中出现多少次?
Is there a simple way to check how many times a character appears in a String?
推荐答案
您可以删除字符串中的任何其他字符并检查其长度:
You could remove any other character in the string and check the length:
str.replace(/[^a]/g, "").length
在这里计算 str
中有多少 a
个. RegExp
如下所述:
Here it is counted how many a
s are in str
. The RegExp
is described below:
[ // Start Character Group
^ // Not operator in character group
a // The character "a"
] // End character group
这篇关于JavaScript:字符在字符串中出现多少次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文