/*
JavaScript Unleashed, Third Edition
by Richard Wagner and R. Allen Wyke
ISBN: 067231763X
Publisher Sams CopyRight 2000
*/
<html>
<head>
<title>JavaScript Unleashed</title>
<script language="JavaScript1.1" type="text/javascript">
<!--
// Define a new method for the String object
String.prototype.replace = stringReplace;
function stringReplace(findText, replaceText) {
// Had to add this variable to inherit calling object
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;
}
//-->
</script>
</head>
<body>
<script language="JavaScript1.1" type="text/javascript">
<!--
// Declare variables
var origString = new String("Allen");
var findString = new String("All");
var replaceString = new String("Ell");
// Notice the difference here
var resultString = origString.replace(findString, replaceString)
// Write results to the page
document.write("The original string was: " + origString + "<br>");
document.write("We searched for: " + findString + "<br>");
document.write("We replaced it with: " + replaceString + "<hr size='1'>");
document.write("The result was: " + resultString);
//-->
</script>
</body>
</html>
|