I hope this tip will come in handy for all you JavaScripters out there!
Last week I was reading this tip on SearchDomino http://searchdomino.techtarget.com/tip/1,289483,sid4_gci816795,00.html. And at the bottom was the following comment: "The below are various JavaScript equivalent of LotusScript functions, taken from various sources - mostly from the "JavaScript Bible 3rd edition" by Danny Goodman"
...followed by this function...
function replaceSubstring (inputString, badString,
goodString, caseSensitive) {
fixedReplace = "";
UI = inputString;
UB = badString;
if ((caseSensitive != 1) && (caseSensitive != true)) {
UI = inputString.toUpperCase();
UB = badString.toUpperCase();
}
badEnd = -1;
badLoc = UI.indexOf(UB);
if (badLoc != -1) {
for (x=1; (badLoc != -1); x++) {
fixedReplace = fixedReplace +
inputString.substring((badEnd +
1), badLoc) + goodString
badEnd = badLoc + UB.length - 1;
badLoc = UI.indexOf(UB, (badLoc + 1)); }
fixedReplace = fixedReplace +
inputString.substring((badEnd + 1),
inputString.length); }
else { fixedReplace = inputString; }
return fixedReplace;
}
Now I want to say up front that I think *Danny Goodman is awesome*, and he really knows his stuff. I have learned a lot from his writings, and encourage others to seek out good examples like his.
That said, when I saw the example function given for replacing strings, I recognized that my own little succinct function, which I have continued to improve over the past few months, might come in handy to other people.
My "replacestring" function includes:
*Proper handling of "" as a "search for" string (common overlooked
case in other examples I have seen)
*Case-insensitivity (with the default being case SENSITIVE)
*Missing parameter checks so this is handled intelligently instead
of spitting out "garbage" results.
*Really efficient coding, I did lots of timing checks and
.split//.join is just as fast as "regular expressions"
*No knowledge of "regular expressions" required!
*Even includes a "one-line" version for easy pasting!
So now I feel this is the absolutely most speedy, efficient, error-proof JavaScript "replacestring" function that I could write, handling all the cases you could possibly encounter in your JavaScripting needs.
Code
*Offered freely by Darren J Semotiuk, Collaborative Learning Network Inc., http:////www.co-learn.net/ Just keep this comment line intact. Thanks! *
function replacestring(str_normal,str_find,str_replace,int_case_insensitive)
{
if (arguments.length<3 || str_find=="" || str_normal=="" ||
typeof("".split)!="function")
return(str_normal);
//no parm means default, "case SENSITIVE"...
if(!(int_case_insensitive))
return(str_normal.split(str_find)).join(str_replace);
str_find=str_find.toLowerCase();
var rv="";
var ix=str_normal.toLowerCase().indexOf(str_find);
while(ix>-1)
{
rv+=str_normal.substring(0,ix)+str_replace;
str_normal=str_normal.substring(ix+str_find.length);
ix=str_normal.toLowerCase().indexOf(str_find);
};
return(rv+str_normal);
};//end function replacestring
OR
better yet, a one-liner you paste in ANYWHERE...
function rs(sd,sf,sr,ic){if(arguments.length<3||sf==""||sd==""||typeof("".split)
!="function")return(sd); /*no parm means default, "case
SENSITIVE"...*/if(!(ic))return(sd.split(sf)).join(sr);
sf=sf.toLowerCase();var rv="";var
ix=sd.toLowerCase().indexOf(sf);while(ix>-1)
{rv+=sd.substring(0,ix)+sr;sd=sd.substring(ix+sf.length);ix=sd.to
LowerCase
().indexOf(sf);};return(rv+sd);};
Enjoy! I hope this saves people the trouble of reinventing the wheel all the time. Darren Semotiuk