51
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[JavaScript]文字列中に含まれるある文字の個数を簡単に返す。

Posted at

ちょっと思いついたので、メモ。


var counter = function(str,seq){
    return str.split(seq).length - 1;
}

var str = "Qiita";
var str2 = "Qiiiiiiiiiiiiiiiiiiita";
var str3 = "a1b2c3d4e5f"

console.log(str+":"+counter(str,"i")); 
console.log(str2+":"+counter(str2,"i")); 
//正規表現も使える
console.log(str3+":"+counter(str3,/\d/)); 
console.log(str3+":"+counter(str3,/[a-z]/)); 


splitなので正規表現も使えます。

確認する[runstant]

51
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
51
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?