<html><head> <title>Adding a replace() Method to the String Object</title></head><body> <script language="JavaScript1.1" type="text/javascript"> <!-- String.prototype.replace = stringReplace; function stringReplace(findText, replaceText) { var originalString = new String(this); var pos = 0; var len = findText.length; pos = originalString.indexOf(findText); while (pos != -1) { preString = originalString.substring(0, pos); postString = originalString.substring(pos + len,originalString.length); originalString = preString + replaceText + postString; pos = originalString.indexOf(findText); } return originalString; } var origString = new String("This is a test"); var findString = new String("i"); var replaceString = new String("I"); var resultString = origString.replace(findString, replaceString) document.write("The original string was: " + origString + "<br>"); document.write("We searched for: " + findString + "<br>"); document.write("We replaced it with: " + replaceString + "<br>"); document.write("The result was: " + resultString); //--> </script></body></html>
Name (required)
email (will not be published) (required)
Website