0

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:

Screenshot showing lots of new lines

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.

isherwood
  • 58,414
  • 16
  • 114
  • 157

1 Answers1

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.

  • Hello, thank you for your reply! I am trying to recreate https://discord.com (Most servers would have a chat robot that prevents spam). The thing is, I allow \n to be used. I just would like to cap it for about 5 times in a row. Thanks! – Liam Sholts Apr 13 '22 at 13:28
  • does this similar to your problem? https://stackoverflow.com/questions/29481088/how-can-i-tell-if-a-string-repeats-itself-in-python – Lutfi Fitroh Hadi Apr 13 '22 at 13:38
  • Unfortunately, that is in python. I am using javascript (you can tell from the tags I put). I'll mention this in the original question. Also, instead of knowing if it repeats, I would like it to just fix the string. If the original string is "AAAAAAAAAA" it would be sent as "AAAAA". Thanks! – Liam Sholts Apr 13 '22 at 13:40
  • Yes, I'm aware that is in python. What I was asking though, is about the concept of the problem, whether it is similar to yours. Because some of the answers there are using regex, so if the problem is similar, I guess you can grab the pattern and tweak it according to your need. – Lutfi Fitroh Hadi Apr 13 '22 at 13:48
  • Updated my answers to add a regex pattern to find the repetition of 5 occurrences or more. – Lutfi Fitroh Hadi Apr 13 '22 at 14:00