First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Kory Roberts | Website: http://www.k2w.f2s.com/

The Details Part 3

We will now break apart the code that will replace work magic forward! from the previous page. Let's first assume that the function is called using the following:

string_out = searchandreplace("The dog is chasing the mouse. The dog loves to eat mice.", "dog", "cat");

For the line-by-line breakdown I will be giving the code, then a short description of what is happening.

var pos = the_string.indexOf(search_string); 

Set the variable pos to the beginning index of where the first "dog" is located in the string. Since the first character "T" has an index of 0, pos is now set to 4. This corresponds to the location of the first character in the search string. If the search string cannot be found in the string, pos is set to -1 and the following loop is never executed.

while (pos >= 0) {

Start a loop that will stop when the variable pos is less than 0.


Increment the variable found. This is the same as found = found + 1.

   var start_string = the_string.substr(0, pos);

Start breaking up the string by first finding the beginning section of the string. The variable start_string is set to "The ".

   var end_string = the_string.substr(pos + search_string.length);

Find the ending section of the string. The variable end_string is set to " is chasing the mouse. The dog loves to eat mice.".

   the_string = start_string + replace_string + end_string;

Put the string back together, adding in the replace string between the start string and end string. You've just replaced the first occurrence of "dog" with "cat"! The string is now "The cat is chasing the mouse. The dog loves to eat mice." Notice that the second occurrence of "dog" has not yet been replaced.

   pos = the_string.indexOf(search_string, pos + replace_string.length);

Set the variable pos to the beginning index of the next "dog", but begin after the replacement string (just in case the replacement string includes the word "dog" and we end up in an endless loop). Since there is one more occurrence of the word "dog", we will start back at the while loop and go through one more time. Once "dog" cannot be found, pos is set to -1 and the looping stops.

   if (found == occurrences) {
      pos = -1;

Check to see if we need to stop looking for occurrences. Once we have found all of the occurrences specified by the argument occurrences, we can set pos to -1 and stop the looping. If the argument occurrences is not given, all occurrences are replaced. This is because occurrences now equals null and found == occurrences will never be true.


Just add that last curly bracket to close up the loop.

» Level Advanced

Added: 2001-01-08
Rating: 8 Votes: 38
(10 being the highest)
» Author
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.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!


  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.