<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Prototype JavaScript framework - String</title>
  <id>tag:prototypejs.org,2011:mephisto/api/string</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://prototypejs.org/feed/api/string/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://prototypejs.org/api/string" rel="alternate" type="text/html"/>
  <updated>2008-07-24T08:31:40Z</updated>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org,2007-10-23:17572</id>
    <published>2007-10-23T16:40:00Z</published>
    <updated>2008-07-24T08:31:40Z</updated>
    <category term="String"/>
    <category term="1.6"/>
    <link href="http://prototypejs.org/api/string/interpolate" rel="alternate" type="text/html"/>
    <title>interpolate</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;interpolate(object[, pattern]) -&gt; string&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Treats the string as a Template and fills it with &lt;code&gt;object&lt;/code&gt;&#8217;s properties.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;interpolate(object[, pattern]) -&gt; string&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Treats the string as a Template and fills it with &lt;code&gt;object&lt;/code&gt;&#8217;s properties.&lt;/p&gt;
&lt;p&gt;Equivalent to calling &lt;a href=&quot;/api/template/evaluate&quot;&gt;&lt;code&gt;evaluate&lt;/code&gt;&lt;/a&gt; on a previously-instantiated &lt;a href=&quot;/api/template&quot;&gt;&lt;code&gt;Template&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
&quot;#{animals} on a #{transport}&quot;.interpolate({ animals: &quot;Pigs&quot;, transport: &quot;Surfboard&quot; });
//-&gt; &quot;Pigs on a Surfboard&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var syntax = /(^|.|\r|\n)(\&amp;lt;%=\s*(\w+)\s*%\&amp;gt;)/; //matches symbols like '&amp;lt;%= field %&amp;gt;'
var html = '&amp;lt;div&amp;gt;Name: &amp;lt;b&amp;gt;&amp;lt;%= name %&amp;gt;&amp;lt;/b&amp;gt;, Age: &amp;lt;b&amp;gt;&amp;lt;%=age%&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;/div&amp;gt;';
html.interpolate({ name: 'John Smith', age: 26 }, syntax);
// -&gt; &amp;lt;div&amp;gt;Name: &amp;lt;b&amp;gt;John Smith&amp;lt;/b&amp;gt;, Age: &amp;lt;b&amp;gt;26&amp;lt;/b&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:prototypejs.org,2007-06-20:16236</id>
    <published>2007-06-20T14:39:00Z</published>
    <updated>2007-06-20T14:54:17Z</updated>
    <category term="String"/>
    <category term="1.5.1.1"/>
    <link href="http://prototypejs.org/api/string/isJSON" rel="alternate" type="text/html"/>
    <title>isJSON</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;isJSON() -&gt; boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Check if the string is valid JSON by the use of regular expressions. This security method is called internally.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;isJSON() -&gt; boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Check if the string is valid JSON by the use of regular expressions. This security method is called internally.&lt;/p&gt;
&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
&quot;something&quot;.isJSON()
// -&gt; false
&quot;\&quot;something\&quot;&quot;.isJSON()
// -&gt; true
&quot;{ foo: 42 }&quot;.isJSON()
// -&gt; false
&quot;{ \&quot;foo\&quot;: 42 }&quot;.isJSON()
// -&gt; true
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Tobie</name>
    </author>
    <id>tag:prototypejs.org,2007-05-01:15388</id>
    <published>2007-05-01T16:58:00Z</published>
    <updated>2007-05-01T17:20:44Z</updated>
    <category term="String"/>
    <category term="1..5.1"/>
    <link href="http://prototypejs.org/api/string/unfilterjson" rel="alternate" type="text/html"/>
    <title>unfilterJSON</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;unfilterJSON([filter = Prototype.JSONFilter]) -&gt; String&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Strips comment delimiters around Ajax JSON or JavaScript responses. This security method is called internally.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;unfilterJSON([filter = Prototype.JSONFilter]) -&gt; String&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Strips comment delimiters around Ajax JSON or JavaScript responses. This security method is called internally.&lt;/p&gt;
&lt;h3&gt;Example&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
'/*-secure-\n{&quot;name&quot;: &quot;Violet&quot;, &quot;occupation&quot;: &quot;character&quot;, &quot;age&quot;: 25}\n*/'.unfilterJSON()
// -&gt; '{&quot;name&quot;: &quot;Violet&quot;, &quot;occupation&quot;: &quot;character&quot;, &quot;age&quot;: 25}'
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:prototypejs.org,2007-03-09:14751</id>
    <published>2007-03-09T10:59:00Z</published>
    <updated>2007-03-09T19:03:13Z</updated>
    <category term="String"/>
    <category term="1.5.1"/>
    <link href="http://prototypejs.org/api/string/empty" rel="alternate" type="text/html"/>
    <title>empty</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;empty() -&gt; Boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Checks if the string is empty.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;empty() -&gt; Boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Checks if the string is empty.&lt;/p&gt;
&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
''.empty();
//-&gt; true

'  '.empty();
//-&gt; false
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:prototypejs.org,2007-03-09:14752</id>
    <published>2007-03-09T10:59:00Z</published>
    <updated>2007-03-09T18:50:57Z</updated>
    <category term="String"/>
    <category term="1.5.1"/>
    <link href="http://prototypejs.org/api/string/endsWith" rel="alternate" type="text/html"/>
    <title>endsWith</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;endsWith(substring) -&gt; Boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Checks if the string ends with &lt;code&gt;substring&lt;/code&gt;.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;endsWith(substring) -&gt; Boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Checks if the string ends with &lt;code&gt;substring&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Example&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
'slaughter'.endsWith('laughter')
// -&gt; true
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:prototypejs.org,2007-03-09:14753</id>
    <published>2007-03-09T10:59:00Z</published>
    <updated>2007-03-09T19:02:22Z</updated>
    <category term="String"/>
    <category term="1.5.1"/>
    <link href="http://prototypejs.org/api/string/blank" rel="alternate" type="text/html"/>
    <title>blank</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;blank() -&gt; Boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Check if the string is 'blank', meaning either empty or containing only whitespace.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;blank() -&gt; Boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Check if the string is 'blank', meaning either empty or containing only whitespace.&lt;/p&gt;
&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
''.blank();
//-&gt; true

'  '.blank();
//-&gt; true

' a '.blank();
//-&gt; false
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:prototypejs.org,2007-03-09:14748</id>
    <published>2007-03-09T10:54:00Z</published>
    <updated>2007-03-10T00:49:18Z</updated>
    <category term="String"/>
    <category term="1.5.1"/>
    <link href="http://prototypejs.org/api/string/toJSON" rel="alternate" type="text/html"/>
    <title>toJSON</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;toJSON() -&gt; String&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns a JSON string.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;toJSON() -&gt; String&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns a JSON string.&lt;/p&gt;
&lt;h3&gt;Example&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
'The &quot;Quoted&quot; chronicles'.toJSON();
//-&gt; '&quot;The \&quot;Quoted\&quot; chronicles&quot;'
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:prototypejs.org,2007-03-09:14749</id>
    <published>2007-03-09T10:54:00Z</published>
    <updated>2008-01-18T23:07:43Z</updated>
    <category term="String"/>
    <category term="1.5.1"/>
    <link href="http://prototypejs.org/api/string/evalJSON" rel="alternate" type="text/html"/>
    <title>evalJSON</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;evalJSON([sanitize = false]) -&gt; object&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Evaluates the JSON in the string and returns the resulting object. If the optional &lt;code&gt;sanitize&lt;/code&gt; parameter is set to &lt;code&gt;true&lt;/code&gt;, the string is checked for possible malicious attempts and &lt;code&gt;eval&lt;/code&gt; is not called if one is detected.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;evalJSON([sanitize = false]) -&gt; object&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Evaluates the JSON in the string and returns the resulting object. If the optional &lt;code&gt;sanitize&lt;/code&gt; parameter is set to &lt;code&gt;true&lt;/code&gt;, the string is checked for possible malicious attempts and &lt;code&gt;eval&lt;/code&gt; is not called if one is detected.&lt;/p&gt;
&lt;p class=&quot;notice&quot;&gt;If the JSON string is not well formated or if a malicious attempt is detected a &lt;code&gt;SyntaxError&lt;/code&gt; is thrown.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var person = '{ &quot;name&quot;: &quot;Violet&quot;, &quot;occupation&quot;: &quot;character&quot; }'.evalJSON();
person.name;
//-&gt; &quot;Violet&quot;

person = 'grabUserPassword()'.evalJSON(true);
//-&gt; SyntaxError: Badly formed JSON string: 'grabUserPassword()'

person = '/*-secure-\n{&quot;name&quot;: &quot;Violet&quot;, &quot;occupation&quot;: &quot;character&quot;}\n*/'.evalJSON()
person.name;
//-&gt; &quot;Violet&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Note&lt;/h3&gt;

&lt;p&gt;Always set the &lt;code&gt;sanitize&lt;/code&gt; parameter to &lt;code&gt;true&lt;/code&gt; for data coming from externals sources to prevent XSS attacks.&lt;/p&gt;

&lt;p&gt;As &lt;code&gt;String#evalJSON&lt;/code&gt; internally calls &lt;a href=&quot;unfilterJSON&quot;&gt;&lt;code&gt;String#unfilterJSON&lt;/code&gt;&lt;/a&gt;, optional security comment delimiters (defined in &lt;code&gt;Prototype.JSONFilter&lt;/code&gt;) are automatically removed.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:prototypejs.org,2007-03-09:14750</id>
    <published>2007-03-09T10:54:00Z</published>
    <updated>2007-03-09T18:54:46Z</updated>
    <category term="String"/>
    <category term="1.5.1"/>
    <link href="http://prototypejs.org/api/string/include" rel="alternate" type="text/html"/>
    <title>include</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;include(substring) -&gt; Boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Check if the string contains a substring.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;include(substring) -&gt; Boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Check if the string contains a substring.&lt;/p&gt;
&lt;h3&gt;Example&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
'Prototype framework'.include('frame');
//-&gt; true
'Prototype framework'.include('frameset');
//-&gt; false
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:prototypejs.org,2007-03-09:14754</id>
    <published>2007-03-09T10:54:00Z</published>
    <updated>2007-03-09T18:51:50Z</updated>
    <category term="String"/>
    <category term="1.5.1"/>
    <link href="http://prototypejs.org/api/string/startsWith" rel="alternate" type="text/html"/>
    <title>startsWith</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;startsWith(substring) -&gt; Boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Checks if the string starts with &lt;code&gt;substring&lt;/code&gt;.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;startsWith(substring) -&gt; Boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Checks if the string starts with &lt;code&gt;substring&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Example&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
'Prototype JavaScript'.startsWith('Pro');
//-&gt; true
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:prototypejs.org,2007-03-09:14747</id>
    <published>2007-03-09T10:52:00Z</published>
    <updated>2007-03-09T19:00:38Z</updated>
    <category term="String"/>
    <category term="1.5.1"/>
    <link href="http://prototypejs.org/api/string/times" rel="alternate" type="text/html"/>
    <title>times</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;times(count) -&gt; string&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Concatenates the string &lt;code&gt;count&lt;/code&gt; times.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;times(count) -&gt; string&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Concatenates the string &lt;code&gt;count&lt;/code&gt; times.&lt;/p&gt;
&lt;h3&gt;Example&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
&quot;echo &quot;.times(3);
//-&gt; &quot;echo echo echo &quot;
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Tobie</name>
    </author>
    <id>tag:prototypejs.org,2007-01-03:12934</id>
    <published>2007-01-03T19:19:00Z</published>
    <updated>2007-01-08T17:01:56Z</updated>
    <category term="String"/>
    <link href="http://prototypejs.org/api/string/inspect" rel="alternate" type="text/html"/>
    <title>inspect</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;inspect([useDoubleQuotes = false]) -&gt; String&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns a debug-oriented version of the string (i.e. wrapped in single or double quotes, with backslashes and quotes escaped). &lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;inspect([useDoubleQuotes = false]) -&gt; String&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns a debug-oriented version of the string (i.e. wrapped in single or double quotes, with backslashes and quotes escaped). &lt;/p&gt;
&lt;p&gt;For more information on &lt;code&gt;inspect&lt;/code&gt; methods, see &lt;a href=&quot;/api/object/inspect&quot;&gt;&lt;code&gt;Object.inspect&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
'I\'m so happy.'.inspect();
// -&gt; '\'I\\\'m so happy.\'' (displayed as 'I\'m so happy.' in an alert dialog or the console)

'I\'m so happy.'.inspect(true);
// -&gt; '&quot;I'm so happy.&quot;'  (displayed as &quot;I'm so happy.&quot; in an alert dialog or the console)
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Tobie</name>
    </author>
    <id>tag:prototypejs.org,2007-01-03:12932</id>
    <published>2007-01-03T15:28:00Z</published>
    <updated>2007-02-24T19:09:31Z</updated>
    <category term="String"/>
    <link href="http://prototypejs.org/api/string/scan" rel="alternate" type="text/html"/>
    <title>scan</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;scan(pattern, iterator) -&gt; string&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Allows iterating over every occurrence of the given pattern (which can be a string or a regular expression). Returns the original string.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;scan(pattern, iterator) -&gt; string&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Allows iterating over every occurrence of the given pattern (which can be a string or a regular expression). Returns the original string.&lt;/p&gt;
&lt;p&gt;Internally just calls &lt;a href=&quot;/api/string/gsub&quot;&gt;&lt;code&gt;String#gsub&lt;/code&gt;&lt;/a&gt; passing it &lt;code&gt;pattern&lt;/code&gt; and &lt;code&gt;iterator&lt;/code&gt; as arguments.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
'apple, pear &amp; orange'.scan(/\w+/, alert);
// -&gt; 'apple pear orange' (and displays 'apple', 'pear' and 'orange' in three successive alert dialogs)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Can be used to populate an array:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var fruits = [];
'apple, pear &amp; orange'.scan(/\w+/, function(match){ fruits.push(match[0])});
fruits.inspect()
// -&gt; ['apple', 'pear', 'orange']
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or even to work on the DOM:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
'failure-message, success-message &amp; spinner'.scan(/(\w|-)+/, Element.toggle)
// -&gt; 'failure-message, success-message &amp; spinner' (and toggles the visibility of each DOM element)
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Note&lt;/h3&gt;

&lt;p&gt;Do &lt;em&gt;not&lt;/em&gt; use the &lt;code&gt;&quot;g&quot;&lt;/code&gt; flag on the regex as this will create an infinite loop.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Tobie</name>
    </author>
    <id>tag:prototypejs.org,2007-01-03:12931</id>
    <published>2007-01-03T14:46:00Z</published>
    <updated>2007-02-24T19:10:15Z</updated>
    <category term="String"/>
    <link href="http://prototypejs.org/api/string/sub" rel="alternate" type="text/html"/>
    <title>sub</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;sub(pattern, replacement[, count = 1]) -&gt; string&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns a string with the &lt;em&gt;first&lt;/em&gt; &lt;code&gt;count&lt;/code&gt; occurrences of  &lt;code&gt;pattern&lt;/code&gt; replaced by either a regular string, the returned value of a function or a &lt;a href=&quot;/api/template&quot;&gt;&lt;code&gt;Template&lt;/code&gt; string&lt;/a&gt;. &lt;code&gt;pattern&lt;/code&gt; can be a string or a regular expression.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;sub(pattern, replacement[, count = 1]) -&gt; string&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns a string with the &lt;em&gt;first&lt;/em&gt; &lt;code&gt;count&lt;/code&gt; occurrences of  &lt;code&gt;pattern&lt;/code&gt; replaced by either a regular string, the returned value of a function or a &lt;a href=&quot;/api/template&quot;&gt;&lt;code&gt;Template&lt;/code&gt; string&lt;/a&gt;. &lt;code&gt;pattern&lt;/code&gt; can be a string or a regular expression.&lt;/p&gt;
&lt;p&gt;Unlike  &lt;a href=&quot;/api/string/gsub&quot;&gt;&lt;code&gt;String#gsub&lt;/code&gt;&lt;/a&gt;, &lt;code&gt;String#sub&lt;/code&gt; takes a third optional parameter which specifies the number of occurrences of the pattern which will be replaced. If not specified, it will default to 1.&lt;/p&gt;

&lt;p&gt;Apart from that, &lt;code&gt;String#sub&lt;/code&gt; works just like &lt;a href=&quot;/api/string/gsub&quot;&gt;&lt;code&gt;String#gsub&lt;/code&gt;&lt;/a&gt;. Please refer to it for a complete explanation.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var fruits = 'apple pear orange';

 fruits.sub(' ', ', ');
// -&gt; 'apple, pear orange'

 fruits.sub(' ', ', ', 1);
// -&gt; 'apple, pear orange'

 fruits.sub(' ', ', ', 2);
// -&gt; 'apple, pear, orange'

fruits.sub(/\w+/, function(match){return match[0].capitalize() + ','}, 2);
// -&gt; 'Apple, Pear, orange'

var markdown = '![a pear](/img/pear.jpg) ![an orange](/img/orange.jpg)';

markdown.sub(/!\[(.*?)\]\((.*?)\)/, function(match){
  return '&amp;lt;img alt=&quot;' + match[1] + '&quot; src=&quot;' + match[2] + '&quot; /&amp;gt;';
});
// -&gt; '&amp;lt;img alt=&quot;a pear&quot; src=&quot;/img/pear.jpg&quot; /&amp;gt; ![an orange](/img/orange.jpg)'

markdown.sub(/!\[(.*?)\]\((.*?)\)/, '&amp;lt;img alt=&quot;#{1}&quot; src=&quot;#{2}&quot; /&amp;gt;');
// -&gt; '&amp;lt;img alt=&quot;a pear&quot; src=&quot;/img/pear.jpg&quot; /&amp;gt; ![an orange](/img/orange.jpg)'
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Note&lt;/h3&gt;

&lt;p&gt;Do &lt;em&gt;not&lt;/em&gt; use the &lt;code&gt;&quot;g&quot;&lt;/code&gt; flag on the regex as this will create an infinite loop.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org/">
    <author>
      <name>Tobie</name>
    </author>
    <id>tag:prototypejs.org,2007-01-03:12922</id>
    <published>2007-01-03T05:23:00Z</published>
    <updated>2007-02-24T19:11:23Z</updated>
    <category term="String"/>
    <link href="http://prototypejs.org/api/string/gsub" rel="alternate" type="text/html"/>
    <title>gsub</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;gsub(pattern, replacement) -&gt; string&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the string with &lt;em&gt;every&lt;/em&gt; occurence of a given pattern replaced by either a regular string, the returned value of a function or a &lt;a href=&quot;/api/template&quot;&gt;&lt;code&gt;Template&lt;/code&gt; string&lt;/a&gt;. The pattern can be a string or a regular expression.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;gsub(pattern, replacement) -&gt; string&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the string with &lt;em&gt;every&lt;/em&gt; occurence of a given pattern replaced by either a regular string, the returned value of a function or a &lt;a href=&quot;/api/template&quot;&gt;&lt;code&gt;Template&lt;/code&gt; string&lt;/a&gt;. The pattern can be a string or a regular expression.&lt;/p&gt;
&lt;p&gt;If its second argument is a string &lt;code&gt;String#gsub&lt;/code&gt; works just like the native JavaScript method &lt;code&gt;replace()&lt;/code&gt; set to global match.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var mouseEvents = 'click dblclick mousedown mouseup mouseover mousemove mouseout';

mouseEvents.gsub(' ', ', ');
// -&gt; 'click, dblclick, mousedown, mouseup, mouseover, mousemove, mouseout'

mouseEvents.gsub(/\s+/, ', ');
// -&gt; 'click, dblclick, mousedown, mouseup, mouseover, mousemove, mouseout'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you pass it a function, it will be invoked for every occurrence of the pattern with the match of the current pattern as its unique argument. Note that this argument is the returned value of the &lt;code&gt;match()&lt;/code&gt; method called on the current pattern. It is in the form of an array where the first element is the entire match and every subsequent one corresponds to a parenthesis group in the regex.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
mouseEvents.gsub(/\w+/, function(match){return 'on' + match[0].capitalize()});
// -&gt; 'onClick onDblclick onMousedown onMouseup onMouseover onMousemove onMouseout'

var markdown = '![a pear](/img/pear.jpg) ![an orange](/img/orange.jpg)';

markdown.gsub(/!\[(.*?)\]\((.*?)\)/, function(match){
  return '&amp;lt;img alt=&quot;' + match[1] + '&quot; src=&quot;' + match[2] + '&quot; /&amp;gt;';
});
// -&gt; '&amp;lt;img alt=&quot;a pear&quot; src=&quot;/img/pear.jpg&quot; /&amp;gt; &amp;lt;img alt=&quot;an orange&quot; src=&quot;/img/orange.jpg&quot; /&amp;gt;'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Lastly, you can pass &lt;code&gt;String#gsub&lt;/code&gt; a &lt;a href=&quot;/api/template&quot;&gt;&lt;code&gt;Template&lt;/code&gt; string&lt;/a&gt; in which you can also access the returned value of the &lt;code&gt;match()&lt;/code&gt; method using the ruby inspired notation: &lt;code&gt;#{0}&lt;/code&gt; for the first element of the array, &lt;code&gt;#{1}&lt;/code&gt; for the second one, and so on.  So our last example could be easily re-written as:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
markdown.gsub(/!\[(.*?)\]\((.*?)\)/, '&amp;lt;img alt=&quot;#{1}&quot; src=&quot;#{2}&quot; /&amp;gt;');
// -&gt; '&amp;lt;img alt=&quot;a pear&quot; src=&quot;/img/pear.jpg&quot; /&amp;gt; &amp;lt;img alt=&quot;an orange&quot; src=&quot;/img/orange.jpg&quot; /&amp;gt;'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you need an equivalent to &lt;code&gt;String#gsub&lt;/code&gt; but without global match set on, try  &lt;a href=&quot;/api/string/sub&quot;&gt;&lt;code&gt;String#sub&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Note&lt;/h3&gt;

&lt;p&gt;Do &lt;em&gt;not&lt;/em&gt; use the &lt;code&gt;&quot;g&quot;&lt;/code&gt; flag on the regex as this will create an infinite loop.&lt;/p&gt;
          </content>  </entry>
</feed>

