Tutorials Point


  Learn Prototype
  Prototype Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

Prototype sub() Method


previous next AddThis Social Bookmark Button


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>

To understand it in better way you can Try it yourself.



previous next Printer Friendly

Advertisements


  

Advertisements