How to check number of different characters using regex?
regex
Solution
The following will match a string with a maximum of three different non-space characters
^\s*(\S)?(?:\s|\1)*(\S)?(?:\s|\1|\2)*(\S)?(?:\s|\1|\2|\3)*$
`(\S)` matches one non-space character and captures it so it can then be referenced later in the regex using a back-reference e.g. `\1`. The `?` in the `(\S)?` are used so the string can contain zero, one, two or three types of non-space characters.
The `?:` make a group non-capturing.
The first part of the regex captures up to three different non-space characters `\1`, `\2`, `\3`, and then `(?:\s|\1|\2|\3)*` ensures only those characters or space `\s` can then appear before the end of the string `$`.
One way, in Javascript, to count the number of different non-space characters in a string "using regex":
var str = 'ABC ABC';
var chars = '';
str.replace( /\S/g, function ( m ) {
if ( chars.indexOf(m) == -1 ) chars += m;
});
chars.length; // 3
Problem
I'm trying to create regex to find all inputs containing max three different characters. It doesn't matter how long the input is. Example of cases: - "32 32 32 32 34" --> match - "MM" --> match - " " --> match - "1234" --> no match I've done regex to find inputs of four or more different chars, but now I need it in opposite way... ``` (.).*(?\1)(.).*(?\1)(?\2)(.).*(?\1)(?\2)(?\3)(.) ``` Main question is: How to check number of different characters?