C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » General »

The Essence of essential string replace in Javascript


Posted Date: 12 Apr 2007    Resource Type: Articles    Category: General
Author: Vasudevan Deepak KumarMember Level: Diamond    
Rating: 1 out of 5Points: 10



Introduction


JavaScript has good amount of string handling functions. The most significant and widely used among them in replace function. The replace function, as the name implies, replies a part of the string (technically called as substring) with another string, that we pass as an argument. So far so good. But is it completely robust and comprehensive. This article makes a humble endeavor to discuss the limitations of the function and ways to workaround it and also to extend it.


Limitations


The default JavaScript replace function replaces only the first occurence of the string. So if you are left with iterations of the string matching whole words, sometimes and all sorts of string manipulation juggleries to achieve replace all occurences of a substring with another.


Workaround


There are actually two workarounds that are available to match against the limitations of Javascript string replace function limitation.

1. Using a Regular Expression Replace. You can have the string pattern suffixed with g as a RegExp object. This you can offer to replace with the substring. This way, JavaScript would invoke the RegExp library to replace all occurences of a string with another.

2. A Custom Function: A quick custom function that would operate on itself to match and replace substring all over with the target string.

Custom Function -- Extension


In the typical custom function that we have explained, we can also bring in trim spaces functionality. We can pass " " (single space) as the string to be replaced and "" (no space) as the string that goes in as target string. Now the resulting string would be devoid of any spaces.

Code Example



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Demonstrating String Replace Function In Javascript</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
function ReplacePlus(incomingText,sParam1,sParam2)
{
workString=incomingText;
str1 = sParam1;
str2 = sParam2;
while (workString.indexOf(str1)!=-1)
{
workString=workString.substring(0,workString.indexOf(str1))+str2+workString.substring(workString.indexOf(str1)+str1.length,workString.length);
}
return workString;
}

var strOriginalString = "A quick brown fox jumps over the lazy dog and gets bit by the angry dog making the angry dog to profusely start barking at the now tensed fox.";
var strToReplace = "the";
var strTargetString = strOriginalString.replace(strToReplace,"THE");
alert ("USING BUILTIN STRING REPLACE: \n\n"+strOriginalString + "\n" +strTargetString);
strTargetString = strOriginalString.replace(/the/g, "THE");
alert ("USING REGULAR EXPRESSION REPLACE: \n\n"+strOriginalString + "\n" + strTargetString);
strTargetString = ReplacePlus(strOriginalString, "the", "THE");
alert ("USING CUSTOM ReplacePlus FUNCTION: \n\n"+strOriginalString +"\n"+strTargetString);
//-->
</script>

</body>
</html>




Summary


Hence we discuss the limitations of the default string replace function besides getting a workaround plus a comprehensive string replace and space trimmer function in Javascript.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: A Review on TaskManager Extension Tool from CodeProject
Previous Resource: Comprehensive and Quicker Validation of Email Address
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use