title: how to find the number of occurences of a substring in a string in javascript?
body: <p>I have the following code in javascript.</p>
  
  <pre><code>    var contents = '&lt;p&gt;this is some text.&lt;span class="Am"&gt;&lt;/span&gt;This is som text&lt;span class="Am"&gt;&lt;/span&gt;This is some text&lt;span class="Am"&gt;&lt;/span&gt;&lt;/p&gt;';
  </code></pre>
  
  <p>Now I have to find the number of occurences of span tag inside this string and then use a loop throgh this number. For example</p>
  
  <pre><code> function doStuff(){
          var contents = '&lt;p&gt;this is some text.&lt;span class="Am"&gt;&lt;/span&gt;This is som text&lt;span class="Am"&gt;&lt;/span&gt;This is some text&lt;span class="Am"&gt;&lt;/span&gt;&lt;/p&gt;';
          var spans = 'Number of spans in the string';  // this is required to me
          for(i=1;i&lt;=spans;i++){
              // do some stuff
              }
         }
  </code></pre>
  
  <p>Can anyone tell me how it can be done with javascript ?</p>
  
  <p>Thanks in advance.</p>