Adding a replace() Method to the String Object : String : Language Basics : JavaScript DHTML examples (example source code) Organized by topic

JavaScript DHTML
C++
PHP


JavaScript DHTML  »  Language Basics   » [  String  ]   
 



Adding a replace() Method to the String Object

Please note that some example is only working under IE or Firefox.


/*
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>

           
       
Related examples in the same category
1.  Demo all String methods
2.  String utility: word count, replace and capitalize
3.  Strip Commas
4.  Text Range Search and Replace (IE only)
5.  Counting the Words in a Text String
6.  Reversing a String
7.  Trimming a String Using Regular Expressions
8.  String encode and decode
9.  Capitalizing the First Letter in Each Word of a String
10.   Playing with Strings
11.   Using the String Object's Link Method
12.  Using a for Loop to Reverse a String
13.   Concatenate JavaScript String
14.  String length: number of characters in a string.
15.  String fontcolor(): a string in a specified color
16.  String indexOf(): string position
17.  String Validation
18.  Using Quotes within Strings
19.  Using the String Object
20.  String toUpperCase
21.  Lab for string.replace() and string.search()
22.  Slicing a String
23.  A String Object Prototype
24.  Creating a Custom toString() Method
25.  Reading a Portion of a String
26.  Source Code for a Sample Page That Formats a String Object with the 'a' Tag
27.  Source Code for Our String-Formatting Script
28.   Creating a Function That Will Search and Replace in Strings
29.   Using the indexOf() Method to Find All Occurrences of the Letter e in a Sentence
30.  Methods and Properties of the String Object
31.  Using the indexOf() Method to Find All Occurrences of the Letter e in a Sentence
32.  String match(): returns the text if found
33.  String substr() and substring(): returns a specified part of a string
34.  String toLowerCase() and toUpperCase(): converts a string to lowercase and uppercase
35.  Converting Strings to Upper Case
36.  String encoder
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.