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.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|