是否可以使用 jQuery 从段落中获取相同字母的数量?假设我有这一段:
<div>like this printer</div>
我想获取“i”的字符数,这意味着我应该获取 3 个字符,因为有 3 个“i”。我正在考虑使用 $(div:contains(i)),但现在确定如何实现它。有什么建议么?非常感谢!
最佳答案
//Your string
var str = "I like this printer";
//Shows the count of "i" in your string.
console.log(str.replace(/[^i]/g, "").length);
祝你编码愉快!
关于javascript - 如何使用 jQuery 获取段落中相同字符的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34759164/