Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

Search and Replace Strings

To search a string for a substring and replace that substring within the string you can use the String object's .substring method to find the substring and break up the initial string, and use the concatenation operator (+) to reconstruct the initial string with the replacement substring.

In the example shown below, I search for the phrase "wags it's tail" in the text and replace it with the phrase "wiggles it's ears".

<script>
var initialStr = "a dog growls when it's angry and wags it's tail when it's pleased.";
var searchStr = "wags it's tail";
var loc1 = initialStr.indexOf(searchStr);

var leftStr = initialStr.substring(0,loc1);

var loc2 = searchStr.length + loc1;
var len2 = initialStr.length;

var rightStr = initialStr.substring(loc2,len2);

var finalStr = leftStr + "wiggles it's ears" + rightStr;
alert(finalStr);
</script>

In the code, I use the .indexOf method to get the location of the first character of the search phrase with the main string. Then I use the .substring method to save the left side of the main string.

Next I use the location of the first character of the search phrase along with the .length method to get the location of the end of the search phrase within the main string. Using the location of the end of the search phrase, and the length of the main string, I use the .substring method to save the right side of the main string.

Lastly, I use the concatenation operator to string together the left side of the main string, the replacement phrase, and the right side of the main string. This creates the final text "a dog growls when it's angry and wiggles it's ears when it's pleased."

What if you're not sure that the initial text will contain the searched for substring? The .indexOf method returns -1 if it can't find the value of its parameter in the string, so you would use code similar to that shown below to test the return value and not perform the replace.

<script>
var initialStr = "a dog growls when it's angry and wags it's tail when it's pleased.";
var searchStr = "wags it's tail";
var loc1 = initialStr.indexOf(searchStr);

if(loc1 > -1)
{
   var leftStr = initialStr.substring(0,loc1);
   var loc2 = searchStr.length + loc1;
   var len2 = initialStr.length;
   var rightStr = initialStr.substring(loc2,len2);
   var finalStr = leftStr + "wiggles it's ears" + rightStr;
   alert(finalStr);
}
</script>

One last note: The String object also has a substr method, as opposed to .substring. Whereas the .substring method's parameters are character locations within the string, the substr method's parameters specify the starting character location and the number of characters to be included in the substr.

RSS Feed RSS Feed



Follow Stephen Bucaro Follow @Stephen Bucaro


Web Design Sections

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2016 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268