Thanks, blueprintsxp. Great example on that, and works great on that file, but for some reason when I try to integrate it into my own, it doesn’t like it:
<html>
<body>
<script type=“text/javascript”>
var str = “The Lord of \ he Rings: The Fellowship of the Ring”;
var result = str.replace(new RegExp(/\//gi),“\\”);
document.write(result);
</script>
</body>
</html>
(using w3schools.com/jsref/tryit.asp?filename=tryjsref_replace_regexp, it gives “The Lord of he Rings: The Fellowship of the Ring” - the “t” is missing where I put the slash in the original string)
I also noticed that “.replace” seems to not always grab all of the instances it is referenced and replace them.
So, instead, I found this Substring Replace function from this site:
breakingpar.com/bkp/home.nsf/0/45A5696524D222C387256AFB0013D850
It is awesome and will replace anything, anywhere, for however many times it finds it. In case they ever take it off their site, here it is
:
replaceSubstring(“hellothere”, “l”, “x”) = “hexxothere”
replaceSubstring(“this is a string”, “is”, “x”) = “thx x a string”
replaceSubstring(“Here is a long string”, “”, “xxx”) = “Here is a long string”
replaceSubstring(“Here is a long string”, " “, “”) = “Hereisalongstring”
’ The slash is a literal character, so “\\” is a single slash
replaceSubstring(”\\mysubdir\\mydatabase.nsf", “\\”, “/”) = “/mysubdir/mydatabase.nsf”
’ If the string is “literal \s\w characters”, it must be represented literally like “literal \\s\\w characters”
replaceSubstring(“literal \\s\\w characters”, “\\”, “\\\\”) = “literal \\s\\w characters”
replaceSubstring(“Getting rid of unwanted words”, “unwanted”, “unneeded”) = “Getting rid of unneeded words”
Here is the function:
function replaceSubstring(inputString, fromString, toString) {
// Goes through the inputString and replaces every occurrence of fromString with toString
var temp = inputString;
if (fromString == "") {
return inputString;
}
if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
while (temp.indexOf(fromString) != -1) {
var toTheLeft = temp.substring(0, temp.indexOf(fromString));
var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
temp = toTheLeft + toString + toTheRight;
}
} else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
var midStrings = new Array("~", "`", "_", "^", "#");
var midStringLen = 1;
var midString = "";
// Find a string that doesn't exist in the inputString to be used
// as an "inbetween" string
while (midString == "") {
for (var i=0; i < midStrings.length; i++) {
var tempMidString = "";
for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
if (fromString.indexOf(tempMidString) == -1) {
midString = tempMidString;
i = midStrings.length + 1;
}
}
} // Keep on going until we build an "inbetween" string that doesn't exist
// Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
while (temp.indexOf(fromString) != -1) {
var toTheLeft = temp.substring(0, temp.indexOf(fromString));
var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
temp = toTheLeft + midString + toTheRight;
}
// Next, replace the "inbetween" string with the "toString"
while (temp.indexOf(midString) != -1) {
var toTheLeft = temp.substring(0, temp.indexOf(midString));
var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
temp = toTheLeft + toString + toTheRight;
}
} // Ends the check to see if the string being replaced is part of the replacement string or not
return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function