Prototype - sub() Method


Advertisements


This method returns a string with the first count occurrences of pattern replaced by either a regular string, the returned value of a function or a Template string. Pattern can be a string or a regular expression.

Syntax

string.sub(pattern, replacement[, count = 1]);

Return Value

Returns a string.

Example

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var fruits = 'apple pear orange';
            alert("fruits.sub(' ', ', '):"+  fruits.sub(' ', ', '));
            alert("fruits.sub(' ', ', ', 1): " +  fruits.sub(' ', ', ', 1));
            alert("fruits.sub(' ', ', ', 2): " +  fruits.sub(' ', ', ', 2));
            var str = fruits.sub(/\w+/, 
            function(match) { return match[0].capitalize() + ','}, 2);
            alert(str);

         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      <br />
      <br />
      <input type = "button" value = "Result" onclick = "showResult();"/>
   </body>
</html>

Output


prototype_string_processing.htm

Advertisements