获取JavaScript中出现的字符数 [英] Get number of occurrences of a character in JavaScript
本文介绍了获取JavaScript中出现的字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,使用','
来分配一些数据。现在我想计算该字符串中','
的出现次数。我试过了:
I have a string that devides some data using ','
. Now I want to count the occurences of ','
in that string. I tried:
var match = string.match('/[,]/i');
但这会给我null如果我试图获得匹配数组的长度。任何想法?
But this gives me null If I try to get the length of the match array. Any ideas?
推荐答案
如果你需要检查一个简单模式的出现是,那么最好不要使用常规表达式。
If you need to check the occurances of a simple pattern as "," then better don't use regular expressions.
尝试:
var matchesCount = string.split(",").length - 1;
这篇关于获取JavaScript中出现的字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文