 |
 |
| Domino Tips: |
|
 |
 |

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




|
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...
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 f
To continue reading for free, register below or login
To read more you must become a member of SearchDomino.com
');
// -->

or" 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! *
Enjoy! I hope this saves people the trouble of reinventing the wheel all the time. Darren Semotiuk
 |

|
|
 |
|
 |
 |
 |
 |
| TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of . |
|
| |
All Rights Reserved, , TechTarget |
|
|
|
|
|