'How to prevent repetitive text in a string? [duplicate]
Right now I am experiencing an issue about preventing repetitive letters / words such as new lines "\n\n\n\n", letters "aaaaaaaa", and words "spam spam spam spam".
Here is an example of what I mean of new line in my chat application:
As you can see, this can fill up a chat application if abused. Since I don't really know where to start, I am not going to include any code, as it is not related to this question. I tried looking at some discord bots (I used to be a discord bot programmer) and look at their anti-spam features, but haven't found anything.
I define spam as something that is repeated around 5 times in a row.
Solution 1:[1]
For preventing new lines or whitespaces, you can use the trim() method.
Another way of doing this is using the replace() method and then putting \n for the regex pattern.
However, for the repetitive letters or words, I think you must first define what kind of repetition you don't want. For example, the word "hahahaha" have repetition but this word might be a valid one for a chat application. Furthermore, I don't know what your use case is, but I can't think of any chat app that would ban or prevent something like this. You can repetitively spam a chat on Whatsapp, Telegram, or etc.
---------- UPDATE -----------
To find the repetition of 5 times or more, you can try to use this regex pattern: (.{5,}?)\1+$
Or checkout this regex101: https://regex101.com/r/Of1lnG/1
This might not be perfectly suited for your use case, but I think you can use it as a starter.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

