Alex Mueller on Software and Technology 
Tuesday, December 06, 2005

Working with JavaScript is always a little shady for me. I tend not to trust my JavaScript code at first glance, due to the differences in standards that browsers support, the a lack of a formal, scripting environment, and the kludgy approach to debugging. IE browsers support these standards, while non IE browsers support those, and in some cases both can agree. When working in JavaScript, community support becomes my biggest asset. For those searching for replace all functionality, I present this entry.

After using the JavaScript string's "replace" method, I noticed I was receiving unexpected results, specifically, the end result seemed to be partially complete. I was not sure where to place the blame, so I began debugging, only to find out the following.

The JavaScript string's "replace" method does not replace all occurrences within the string, unlike the .Net string.Replace(). It only replaces the first occurrence. Since the replace function would actually need to recurse the string as an array, searching for matches, and replacing them. JavaScript has not built this into their library. I am going to guess this is the same reason that XSLT does not have a replace function.

In any case, what can be done about the much needed replace all functionality missing in JavaScript? There are a few alternatives, and I present two. One idea would be to create a new function, "replaceAll()." In it, we would use a while loop, and iterate through the string searching for matches and replacing each. The user would pass in a string reference, the text to replace, and the text that will replace it.

function replaceAll(text, strA, strB)
{
    while ( text.indexOf(strA) != -1)
    {
        text = text.replace(strA,strB);
    }
    return text;
}

Another approach involves using regular expressions within JavaScript. Regular expressions, while sometimes difficult to understand, are useful, and in a situation such as this, they reduce the need to write excess JavaScript code, therefore reducing possible errors (in theory). There are number of resources on the internet providing instructions, tutorials, and tools for using regular expressions. In the following code, my "replaceAll()" method will accomplish the same goal using the power of regular expressions.

function replaceAll(text, strA, strB)
{
    return text.replace( new RegExp(strA,"g"), strB );    
}

My wish would be for the JavaScript string's replace method to replace all instances of a string, not just one. In the meantime, here are two, easy to use methods, to finish what JavaScript started. One good resource using regular expressions in JavaScript, as well as with other languages, can be found at Regular-Expressions.info.

Tuesday, December 06, 2005 3:52:54 PM (Mountain Standard Time, UTC-07:00) | Comments [0] | JavaScript#
Comments are closed.
MuellerDesigns.net
Search
On This Page
PowerShell Documentation
Automate Daily Tasks with PowerShell
SketchPath XPath Editor
Software Testing - Revisited
Architecting Buildings and Software
NBCOlympics.com with Silverlight
Marker Interfaces and C# Attributes
The Phone Screen
Working with ASP.NET MVC and MvcContrib
Thanks to BDD
Twitter
The Opposite of a Singleton?
Removing Duplicate Code in Functions
Add Vista Themes to Longhorn
Changing File Ownership In Vista and Longhorn
Most Popular
JavaScript ReplaceAll Functionality
What is polymorphism?
What is composition?
Sorting with IComparable and IComparer
Applying the Observer Pattern in ASP.NET
MVP in ASP.NET
What is abstraction?
What is encapsulation?
What is a class?
What is inheritance?
Authentication in ASP.NET
Calendar Controls
XPathNavigator.CheckValidity new for 2.0
SQL Server 2005 Connection Issues
Auto-attach to process '[####] aspnet_wp.exe' on m...
What is an object?
FreeTextBox
VMWare and VPC
An Example of Reflection using C#
Caring for the Team
Archive
Links
Categories
My Local Blog Map
Blogroll
About
Powered by:

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2009
MuellerDesigns.net

Sign In

Help Those In Need
The Hunger Site
Ronald McDonald House Charities (RMHC) of Western Washington & Alaska