Javascript: Search and Replace
If anybody is having trouble with an annoying javascript String.replace(n1,n2) breakage… Here’s a great work-around the folks at Lombardi lead me to.
Javascript search and replace.
This replace function works very well when trying to fix “busted” HTML pages (caused by the occational bad encoder).
Usage example:
tmpString = replaceIt(tmpString, "<", "<");
tmpString = replaceIt(tmpString, ">", ">");
function replaceIt(sString, sReplaceThis, sWithThis) {
if (sReplaceThis != "" && sReplaceThis != sWithThis) {
var counter = 0;
var start = 0;
var before = "";
var after = "";
while (counter < sString.length) {
start = sString.indexOf(sReplaceThis, counter);
if (start == -1) {
break;
} else {
before = sString.substr(0, start);
after = sString.substr(start + sReplaceThis.length, sString.length);
sString = before + sWithThis + after;
counter = before.length + sWithThis.length;
}
}
}
return sString;
}
Copyright ©2007 Gary Samuelson, All Rights Reserved.