Home > Domino Tips > Developer > JavaScript > JavaScript replacestring function - fast, robust, reusable
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

JAVASCRIPT

JavaScript replacestring function - fast, robust, reusable


Darren Semotiuk
04.26.2002
Rating: -4.07- (out of 5) Hall of fame tip of the month winner


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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

Rate this Tip
To rate tips, you must be a member of SearchDomino.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
JavaScript
How to create dynamic JavaScript in Notes Domino without formulas
Trap an attachment path via the Domino file upload control field
Converting Lotus Notes views to XML documents using JavaScript
Prevent errors on iFramed pages with JavaScript
How to add keyboard functionality for Lotus Notes documents
Validate Lotus Notes Domino fields using JavaScript
How to support Flash objects in any Web browser for a Lotus Notes Domino application
How to validate Lotus Notes forms on a Domino server without losing entered data
Ajax code equivalent of the @DBColumn formula for Lotus Notes
A bevy of Notes/Domino development tips

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Domino & Lotus Notes Security Solutions: Authentication, Antispam, Encryption and Antivirus
HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 1999 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts