Javascript:字符串中出现字符的次数 [英] Javascript: How many times a character occurs in a string

查看:106
本文介绍了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

这里计算了多少 a s在 str

Here it is counted how many as are in str.

这篇关于Javascript:字符串中出现字符的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆