Is there a simple way to check how many times a character appears in a String?
It's because if we want to be able to count any kind of characters then we need to consider two expressions. One for common characters and second for special characters for example like [, ], ^ and so on. It's easy to forget about it, but even if we remember it, I think we're unnecessarily expanding our code.
In this case for string str and character ch works each of these solutions:
let count = str.split(ch).length - 1
(thanks to @Sarfraz)
or
let count = str.split('').filter(x => x == ch).length
or
let count = 0
str.split('').forEach(x => x == ch ? count++ : null)
Enjoy!