A regular expression that checks for the existence of at least 3 different characters in a string

I need a regular expression that ensures that the string contains at least 3 different characters (of any type).

Example: aqaqaq is invalid because it consists of only two different characters. aqwaqa or aq3aqa or aq! aqa.

Is this possible in regular expression?

Languages: Javascript / PHP

thank

+3
source share
1 answer

You can use this regex containing negative images:

/(.).*(?!\1)(.).*(?!\1)(?!\2)(.)/

Examples:

> regex = /(.).*(?!\1)(.).*(?!\1)(?!\2)(.)/
> regex.exec('abab!aba')
["abab!ab", "a", "!", "b"]
> regex.exec('abababa')
null
+4
source

All Articles