The following is the entire function we will be analyzing. Don't worry if it looks a little scary, we will be going through it step-by-step. I just thought it might be good to see the entire thing first. In the example movie, this function is located in Frame 1.
function searchandreplace (the_string, search_string, replace_string, occurrences, backward) {
if (search_string == replace_string) {
return the_string;
}
var found = 0;
if (backward == true) {
var pos = the_string.lastIndexOf(search_string);
while (pos>= 0) {
found++;
var start_string = the_string.substr(0, pos);
var end_string = the_string.substr(pos + search_string.length);
the_string = start_string + replace_string + end_string;
pos = the_string.lastIndexOf(search_string, start_string.length);
if (found == occurrences) {
pos = -1;
}
}
}
else {
var pos = the_string.indexOf(search_string);
while (pos>= 0) {
found++;
var start_string = the_string.substr(0, pos);
var end_string = the_string.substr(pos + search_string.length);
the_string = start_string + replace_string + end_string;
pos = the_string.indexOf(search_string, pos + replace_string.length);
if (found == occurrences) {
pos = -1;
}
}
}
return the_string;
}
Kory Roberts is a part-time Flash developer. He is the author of ScoreKeeper and has experience integrating Flash with other scripting languages, such as Perl/CGI and PHP.