Javascript word switch script

  • sazn
  • Student
  • Student
  • User avatar
  • Posts: 98

Post 3+ Months Ago

ok i think this is possible using javascript. what i want is a script that simply changes certain words in a form box. All the words that i specified would be changed over for example if i had specified that i wanted swear word changed to sw**r w**d then when the user typed in swear word it would be changed to sw**r w**d when they clicked a submit button below the form box. when the user clicked submit what they had just written would not be submited to anywere but would just switch over to show the words in the form box changed to the words i had specified. i'm pretty sure there are no pre made scripts like this available on any sites already.
:D
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I'm trying to force myself to learn regular expressions, so this seemed like good practice. I attempted to replace the vowels in certain words entered into text fields or textareas with asterisks, which is pretty close to what you specified. To avoid posting swear words here I used "your" "bad" and "words" as my test words, which you can replace as you choose separating additional words with the pipe symbol | .

Code: [ Select ]
<html>
<head>
<title>Clean Sweep</title>
<script type="text/javascript">
<!--
var badWords = /your|bad|words/gim;

function clean(f) {

    var el, i;

    for(i = 0; i < f.elements.length; i++) {

        el = f.elements[i];

        if(el.type == "text" || el.type == "textarea") {
            doCheck(el);
        }
    }

    return false;
}

function doCheck(el) {

    var words = el.value.match(badWords);

    if(words!=null){
        for(i=0;i<words.length;i++) {
            el.value = el.value.replace(words[i], words[i].replace(/[aeiou]/gi,"*"));
        }
    }
}
// -->
</script>
</head>
<body>
<form action="" onsubmit="return clean(this)">
<input type="text">
<br><br>
<textarea rows="10" cols="20" name=""></textarea>
<br><br>
<input type="submit">&nbsp;<input type="reset">
</form>
</body>
</html>
  1. <html>
  2. <head>
  3. <title>Clean Sweep</title>
  4. <script type="text/javascript">
  5. <!--
  6. var badWords = /your|bad|words/gim;
  7. function clean(f) {
  8.     var el, i;
  9.     for(i = 0; i < f.elements.length; i++) {
  10.         el = f.elements[i];
  11.         if(el.type == "text" || el.type == "textarea") {
  12.             doCheck(el);
  13.         }
  14.     }
  15.     return false;
  16. }
  17. function doCheck(el) {
  18.     var words = el.value.match(badWords);
  19.     if(words!=null){
  20.         for(i=0;i<words.length;i++) {
  21.             el.value = el.value.replace(words[i], words[i].replace(/[aeiou]/gi,"*"));
  22.         }
  23.     }
  24. }
  25. // -->
  26. </script>
  27. </head>
  28. <body>
  29. <form action="" onsubmit="return clean(this)">
  30. <input type="text">
  31. <br><br>
  32. <textarea rows="10" cols="20" name=""></textarea>
  33. <br><br>
  34. <input type="submit">&nbsp;<input type="reset">
  35. </form>
  36. </body>
  37. </html>


I'm new to regular expressions, so this may very well still need some work. Right now it will replace the words even if they are substrings of a larger word, and there may be bugs that I haven't spotted yet.
  • sazn
  • Student
  • Student
  • User avatar
  • Posts: 98

Post 3+ Months Ago

wow thats pretty cool thanks! just one thing though, is there a way that i can specify what i want each seperate word to be changed to? i know that you've made the script so that it changes all the letters in the bad word to ** apart from the outside letters on each side. could it be changed so that the bad word could be changed to totaly different words different for each bad word?lol i know this probebly sounds pretty pointless but there is a reason! :D
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I'm not sure how to do that exclusively with regular expressions - or if it even can be done that way, so I used a combination of regular expressions and parallel arrays of good and bad words instead. The first word in the badWords array should be replaced by the first word in the goodWords array etc. I had a little trouble with the newlines in the textarea, so I swapped it with a single underscore char before hunting for the words and then swapped it back when I was done. It ain't pretty, but it works for me in IE and Firefox.

Code: [ Select ]
<script type="text/javascript">
<!--
var badWords = new Array( "your", "bad", "words");
var goodWords = new Array("my", "good", "letters");

function clean(f) {
  var el, i;

  for(i = 0; i < f.elements.length; i++) {
   el = f.elements[i];
   if(el.type == "text" || el.type == "textarea") {
     doCheck(el);
   }
  }
  return false;
}

function doCheck(el) {
  var i, j, temp, content;
  var nl2temp = /(\r\n|\r|\n)/g ;
  var temp2nl = /\s_\s/g;

  if(el.type=="textarea") temp = el.value.replace(nl2temp, " _ ");
  else temp = el.value;
  content = temp.split(" ");
   
  for(i = 0; i < content.length; i++) {
   for(j = 0; j < badWords.length; j++) {
     if(content[i] == badWords[j]) {
      content[i] = goodWords[j];    
     }
   }
  }
  temp = content.join(" ");
  if(el.type=="textarea") temp = temp.replace(temp2nl, "\n");
  el.value = temp;
}
// -->
</script>
  1. <script type="text/javascript">
  2. <!--
  3. var badWords = new Array( "your", "bad", "words");
  4. var goodWords = new Array("my", "good", "letters");
  5. function clean(f) {
  6.   var el, i;
  7.   for(i = 0; i < f.elements.length; i++) {
  8.    el = f.elements[i];
  9.    if(el.type == "text" || el.type == "textarea") {
  10.      doCheck(el);
  11.    }
  12.   }
  13.   return false;
  14. }
  15. function doCheck(el) {
  16.   var i, j, temp, content;
  17.   var nl2temp = /(\r\n|\r|\n)/g ;
  18.   var temp2nl = /\s_\s/g;
  19.   if(el.type=="textarea") temp = el.value.replace(nl2temp, " _ ");
  20.   else temp = el.value;
  21.   content = temp.split(" ");
  22.    
  23.   for(i = 0; i < content.length; i++) {
  24.    for(j = 0; j < badWords.length; j++) {
  25.      if(content[i] == badWords[j]) {
  26.       content[i] = goodWords[j];    
  27.      }
  28.    }
  29.   }
  30.   temp = content.join(" ");
  31.   if(el.type=="textarea") temp = temp.replace(temp2nl, "\n");
  32.   el.value = temp;
  33. }
  34. // -->
  35. </script>


If you enter your bad words and then hit submit it should swap the words for my good letters
  • sazn
  • Student
  • Student
  • User avatar
  • Posts: 98

Post 3+ Months Ago

thats exactly what i was looking for thanks alot RichB!!! :D
  • Sulfin
  • Graduate
  • Graduate
  • User avatar
  • Posts: 129
  • Loc: canada

Post 3+ Months Ago

I think I know a shorter way, but I don't know how to make it work for all browsers
You can use the split and join function

Code: [ Select ]
<script>
(textarea).split("bad word").join("replace with this");
</script>
  1. <script>
  2. (textarea).split("bad word").join("replace with this");
  3. </script>

I'm pretty sure that it works.
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Cripes, I'm always making things harder than they have to be:

Code: [ Select ]
<script type="text/javascript">
<!--
var badWords = new Array( "your", "bad", "words");
var goodWords = new Array("my", "good", "letters");

function clean(f) {

  var el, i;

  for(i = 0; i < f.elements.length; i++) {

   el = f.elements[i];
   
   if(el.type == "text" || el.type == "textarea") {
     doCheck(el);
   }
  }
 
  return false;
}

function doCheck(el) {

    var temp, i;

    temp = el.value;

    for(i=0; i<badWords.length; i++) {

        temp = temp.split(badWords[i]);
        temp = temp.join(goodWords[i]);
    }
        
    el.value = temp;
}
// -->
</script>
  1. <script type="text/javascript">
  2. <!--
  3. var badWords = new Array( "your", "bad", "words");
  4. var goodWords = new Array("my", "good", "letters");
  5. function clean(f) {
  6.   var el, i;
  7.   for(i = 0; i < f.elements.length; i++) {
  8.    el = f.elements[i];
  9.    
  10.    if(el.type == "text" || el.type == "textarea") {
  11.      doCheck(el);
  12.    }
  13.   }
  14.  
  15.   return false;
  16. }
  17. function doCheck(el) {
  18.     var temp, i;
  19.     temp = el.value;
  20.     for(i=0; i<badWords.length; i++) {
  21.         temp = temp.split(badWords[i]);
  22.         temp = temp.join(goodWords[i]);
  23.     }
  24.         
  25.     el.value = temp;
  26. }
  27. // -->
  28. </script>


Thanks Sulfin. :D
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I guess there's really no need for a second function now either:

Code: [ Select ]
<script type="text/javascript">
<!--
var badWords = new Array( "your", "bad", "words");
var goodWords = new Array("my", "good", "letters");

function clean(f) {

    var el, i, j, temp;

    for(i = 0; i < f.elements.length; i++) {

        el = f.elements[i];
   
        if(el.type == "text" || el.type == "textarea") {

            temp = el.value;

            for(j=0; j<badWords.length; j++) {

                temp = temp.split(badWords[j]).join(goodWords[j]);
            }
            el.value = temp;
        }
    }
  return false;
}
// -->
</script>
  1. <script type="text/javascript">
  2. <!--
  3. var badWords = new Array( "your", "bad", "words");
  4. var goodWords = new Array("my", "good", "letters");
  5. function clean(f) {
  6.     var el, i, j, temp;
  7.     for(i = 0; i < f.elements.length; i++) {
  8.         el = f.elements[i];
  9.    
  10.         if(el.type == "text" || el.type == "textarea") {
  11.             temp = el.value;
  12.             for(j=0; j<badWords.length; j++) {
  13.                 temp = temp.split(badWords[j]).join(goodWords[j]);
  14.             }
  15.             el.value = temp;
  16.         }
  17.     }
  18.   return false;
  19. }
  20. // -->
  21. </script>
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

//note rude language nearly used in this post! You have been forewarned!

To be fair to sufin, that is a dang nice bit of sideways thinking you've done there :D you have actually made me rather jealous :wink: however I would go for the regular expressions.

Why? Because if you want to remove words, like swear words, you probably also want to remove words that <i>look</i> like those words as well. Take for example the "f-word". What if someone puts in a ù, ú, û, or ü in place of the u. Or they might puposefully mis-spell and leave off the c, or the k. With the split and join you would have to check for every eventuality separately. with regular expressions you could use the following:

Code: [ Select ]
/f[ùúûüu][ck]{1,2}(er|ing|in)?/i


which will check for an f, then any of the "u" variants, then "c", "k" or "ck" (or "kc", "kk" or "cc" come to think of it) with an optional "ing", "in", or "er" on the end. I make that 324 variants total on the single word. You could of course replace the i's with "[i1]" to match 1's as well (current total 540 variants). Plus it's case insensitive (the i on the end) and I'm not gonna work out the number of variations <b>that</b> gives. An example of what this would catch is the word "fúk1N".

The truth is, if you try to censor people, they will instantly respond by trying to get around the censors, and once one person does it, and others see, everyone will be giving it a go! If you're gonna search for naughty language, you might as well do it thoroughly :D

As a side note, the regular expressions will also allow you to do case insensitive matches and single out whole words. For example if you wanted to single out "lots" and change it to "a lot" (because thats what my english teacher told me to do!), you could target just the whole word so you don't turn "shallots" into "shala lot" :lol:

I know it possibly not that important but I just wanted to point out how much more powerful regular expressions are, if you want to code an effective censoring system (which I intend to do quite soon). Anyway, sulfins method is still better for a simple system.
  • Sulfin
  • Graduate
  • Graduate
  • User avatar
  • Posts: 129
  • Loc: canada

Post 3+ Months Ago

What if they put a "-" or a space between the letters? Does it block that also? It would be better if it did that also. I don't really wan't to use my time on it though. :roll:
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Sulfin wrote:
What if they put a "-" or a space between the letters? Does it block that also?

ARRRRGGGGHHHHHH :twisted:

*shakes fist at sulfin*

Thank you for pointing out that I missed like the most obvious thing ever! :roll:

Lol, well It could be adapted to accomodate those but thats one of the more difficult things to implement....

Well like I said I'm thinking of making a badass censoring function anyway, and how I see it, you would enter the word you want filtered and then the system would generate an uber-complicated regular expression, then use that on the text.

As you pointed out, it still needs some work, and it will never be possible to catch everything, but I'm gonna do what I can to catch as much as possible :lol:
  • Sulfin
  • Graduate
  • Graduate
  • User avatar
  • Posts: 129
  • Loc: canada

Post 3+ Months Ago

Yeah, there's always gunna be a loophole in the system no matter how hard we try. But I guess it would be called "success" if we managed to make that hole smaller. 8)

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 88 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.