You can remove repetitions of any character with a simple regular expression, e.g. (.)\1+
However, this will also catch legal uses, for example, words that have doubled letters in their spelling (balloon, spelling, well, etc.).
Thus, you probably want to limit the expression to some forbidden characters, in the end, keeping it as general as possible so that you don't have to change it from time to time, as your users find new characters to use.
One possible solution would be to prohibit repeated characters other than letters and not numbers:
([^A-Za-z0-9])\1+
But even this is not the final solution for all cases, as some of your users may actually use actual letter sequences as separators:
ZZZZZZZZZZZZZZZZZZZZZZ BBBBBBBBBBBBBBBBBBBBBB ZZZZZZZZZZZZZZZZZZZZZZ
To prevent this, and with the added benefit of allowing the legitimate use of certain repeated non-letter characters (for example, in ellipsis: ...), you can limit the repetition of characters to a maximum of 3 using a regular expression with the syntax (<pattern>)\1{min, max}
as follows: (.)\1{4,}
to match offensive character sequences with a minimum length of 4 and an undefined maximum.
source share