Actionscript Code:
myText = "I love to pet my cat. My cat is so nice.";
String.prototype.replaceWord = function(word, new_word, index){
indexes = new Array();
newSentence = this.split("");
newWord = word.split("");
for(i=0;i<newSentence.length;i++){
if(newSentence[i] == newWord[0]){
checkLength = 0;
for(a=0;a<newWord.length;a++){
if(newSentence[i+a] == newWord[a]){
checkLength++;
}
}
if(checkLength == newWord.length){
indexes.push(i);
}
}
}
if(indexes.length > 0){
return this.substring(0, indexes[index])+new_word+this.substring(indexes[index]+word.length, this.length);
} else {
trace("No matches found!");
}
}
myText = myText.replaceWord("cat", "dog", 0);
trace(myText);