Stop offensive words using jquery or javascript

I want to filter out a word (sex, etc.) using a regular expression, but sometimes people use these words as this (bad) (bad) (b / a / d), etc. how to stop these words using regular expression.

This is just one word that I need to filter out all those words that I wrote for this code, but it does not work perfectly

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <script src="jquery-1.8.3.js"></script> <title></title> <meta name="" content=""> </head> <body> </body> <script> $(document).ready(function(){ var a=['bad','worse']; for(var i=0; i<a.length; i++){ var re = new RegExp(a[i],"g"); var str = "bad words here"; var res = str.match(re); if(res.length>0){ alert(res); break; } } }); </script> </html> 
+5
source share
3 answers

Edited: Just use the following code:

 function isAbusive(str) { var badWords = ['bad', 'worst']; var isTrue = false; if(str) { for (var i = 0; i < badWords.length; i++) { isTrue = !!(str.replace(/\W|\s/g, '').toLowerCase().indexOf(badWords[i]) + 1); if(isTrue) break; } } return isTrue; } var comments = document.getElementsByTagName("p"); for (var i = comments.length - 1; i >= 0; i--) { console.log(i, comments[i].innerHTML) if (isAbusive(comments[i].innerHTML)) {//you can check this comment alert("there is bad word"); comments[i].parentNode.removeChild(comments[i]); } } 
 <p class="user1">I want to do "b/a/d" activity</p> <p class="user2">I want to do "worst" activity</p> <p class="user2">I want to do "good" activity</p> <p class="user2">bad activity</p> <p class="user3">W/O/R/s/T activity</p> 
0
source

Try the following:

 return ""; 

This is the only way you ever forbid people to use words that you don’t like: do not let them use any words at all.

Even if you find certain words, you will have many problems with this, many of which were provided to you in the commentary on your question. The exchange of characters, as in @ $$, is too common, and more durable people will know about Cyrillic characters that look identical to Latin characters - try to find sx with your regular expression!

It is infinitely more effective to take a more social approach to this problem. If your community does not wish to publish such words, then there is no need to worry about it. If you soften rare messages containing such words, the offender will not be able to repeat this, and others may think twice before following in their footsteps. I have never had text filters in my user-submitted content, and although we have random handfuls, they are always super-fast to deal with them, and not with watches that will try to stop them in the first place.

+4
source

I REALLY would not advise doing this because he could create some strange situations, such as filtering things that in the context would be completely beautiful, like "this male dog."

Having said that you can do something like ... It should work, it is not ideal, but it will work.

  var filter = ['sex','s/e/x']; // and so on for each bad word. $(document).text(function(i,txt { for (var i = 0; i < filter.length; i++) { var pattern = new RegExp(filter[i], 'gi'); var clean = '*'.repeat(filter[i].length); txt = txt.replace(pattern, clean); } return txt; }); 

Hope this helps!

edit took me so long to write this section of code, I did not see another answer there. Go with this imo, it looks much nicer and more versatile.

0
source

Source: https://habr.com/ru/post/1233217/


All Articles