Confessions and Confusions of Ashok M A. Personal and Professional Blog.

Ashok Mannolu Arunachalam, How toJSRegex

How to find the number of occurrences of chars in a string

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.

// split into each characters.
// Sort it so that the similar chars can be grouped together.
// Join them to group.
// match the chars those have more than one occurrence.
// Do what you wish with the result.
// I am just counting the length here.
.map((a) => a.length)

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