Manage Learn to apply best practices and optimize your operations.

JavaScript replacestring function - fast, robust, reusable

I hope this tip will come in handy for all you JavaScripters out there!

I hope this tip will come in handy for all you JavaScripters out there!

Last week I was reading this tip on SearchDomino 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.

*Offered freely by Darren J Semotiuk, Collaborative Learning Network Inc., http://// 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
sf=sf.toLowerCase();var rv="";var

Enjoy! I hope this saves people the trouble of reinventing the wheel all the time. Darren Semotiuk

This was last published in April 2002

Dig Deeper on Domino Resources - Part 6

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.




  • iSeries tutorials's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...