Recently, I came across a code challenge to find the number of occurrences of a character in a string.

It was intended to use it as a validator in order to perform some minimum char length along with the occurrences.

Anyhow, it took some time to figure out a way using Regex.

1
2
3
4
5
6
7
8
9
10
11
searchString
// split into each characters.
.split('')
// Sort it so that the similar chars can be grouped together.
.sort()
// Join them to group.
.join('')
// match the chars those have more than one occurrence.
.match(/(.)\1+/g)
// Do what you wish with the result. I am just counting the length here.
.map((a) => a.length);

Tip: Try to modify the Regex to match only a set of chars occurrences.