# \[FMX\] indexOf replace words

**URL:** <https://forum.kirupa.com/t/fmx-indexof-replace-words/8277>\
**Category:** classic bestof\
**Created:** [November 17, 2002, 12:46pm UTC](https://forum.kirupa.com/t/fmx-indexof-replace-words/8277 "2002-11-17T12:46:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![andr\_in](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/andr_in/32/37_2.png) [@andr\_in](https://forum.kirupa.com/u/andr_in)\
**Post date:** [November 17, 2002, 12:46pm UTC](https://forum.kirupa.com/t/fmx-indexof-replace-words/8277/1 "2002-11-17T12:46:47Z")

</div>

Input field: “input”  
if (input.indexOf(“word”) == 0) {  
something…  
}  
that detects if there is a word “word” written in the input textbox.  
But is there a way… how can I detect the word and then replace it with \*  
like this: \*\*\*\*  
I just said a bad word… and it was replaced with \*\*\*\* how can I do that?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 18, 2002, 7:46pm UTC](https://forum.kirupa.com/t/fmx-indexof-replace-words/8277/2 "2002-11-18T19:46:44Z")

</div>

here’s a handy prototype i had fun putting together:

```auto

String.prototype.replace = function(find,replace){
var n,r,str = this;
   if(typeof find == "string") find = [find];
   if(typeof replace == "string") replace = [replace];
   n = find.length;
   while(n--){
      if(str.indexOf(find[n]) > -1){
         r = (replace[n] != undefined) ? replace[n] : replace[replace.length-1];
         str = str.split(find[n]).join(r);
      };
   };
   return str;
};

input = "Ah, le con! Il me fait vraiment chier!";
search = ["con","merde","chier"];
replace = ["{one}","{two}","{three}"];
replacedInput = input.replace(search,replace);
// returns "Ah, le {one}! Il me fait vraiment {three}!"

```

this prototype returns a string with the search terms replaced by the replace terms. note that this function is case sensitive.

either search or replace can be an array. if both are arrays, each search term is replaced with it’s matching replace term ( ie. all instances of search[2] would be replaced with replace[2], search[3] with replace[3], etc.). if the search array is longer than the replace array, the last replace item is used.

if search is an array and replace is a string, all instances are replaced with the replace string.

if you want more powerful features, i recommend [pavils jurjans’s RegExp class](http://www.jurjans.lv/flash/RegExp.html), or do your parsing in a server side language!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 18, 2002, 8:00pm UTC](https://forum.kirupa.com/t/fmx-indexof-replace-words/8277/3 "2002-11-18T20:00:39Z")

</div>

This is exactly what I was looking for! 🙂 =)  
thanx man!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 16, 2003, 12:53am UTC](https://forum.kirupa.com/t/fmx-indexof-replace-words/8277/4 "2003-01-16T00:53:40Z")

</div>

I was helping someone who tried to remove characters from a string, and I used this tutorial. I just thought I’d share, even though it’s very close to this prototype:

```auto
//Credits for the prototype: Sbeener
a="This is Cool! Isn't it cool?";
thingsToRemove=["!","?"];
String.prototype.removeChar=function(find){
        var n,str=this;
        if(typeof find == "string") find = [find];
        n=find.length;
        while(n--){
                if(str.indexOf(find[n]) > -1){
                        str = str.split(find[n]).join("");
                };
        };
        return str;
};
trace (a.removeChar(thingsToRemove));
// returns "This is cool Isn't it cool"

```

And I must say that the split.join trick is lovely :beam: I would never have thought of that.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 16, 2003, 2:01am UTC](https://forum.kirupa.com/t/fmx-indexof-replace-words/8277/5 "2003-01-16T02:01:35Z")

</div>

niiiiiice (-:
