Page 1 sur 1

replacesubstring

MessagePublié: 02 Nov 2006 à 18:39
par oguruma
[syntax="javascript"]function myReplaceString(strSource, strFind, strReplace, caseInsensitive){
if (caseInsensitive) {
re = "/" + strFind + "/g";
}
else {
re = "/" + strFind + "/gi";
}
return strSource.replace(eval(re), strReplace);
}
[/syntax]

MessagePublié: 02 Nov 2006 à 18:42
par oguruma
[syntax="javascript"]function replace(sourcelist, fromlist, tolist){
//@Replace( sourcelist ; fromlist ; tolist )
//fromlist and tolist need to be array's
if (fromlist[0] === null || tolist[0] === null) {
alert('fromlist and tolist needs to be arrays')
return sourcelist
}
for (i = 0, i2 = fromlist.length; i < i2; i++) {
//Firt check if sourcelist is array or string
if (sourcelist[0] === null) { // IsString
if (fromlist[i] === sourcelist) {
return tolist[i];
}
}
else { // IsArray
for (j = 0, j2 = sourcelist.length; j < j2; j++) {
if (fromlist[i] === sourcelist[i])
sourcelist[i] = tolist[i];
}
}
}
return sourcelist
}[/syntax]


source : searchdomino

MessagePublié: 02 Nov 2006 à 18:48
par oguruma
[syntax="javascript"]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 < 0) {
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;
}[/syntax]