title: Cannot read property 'length' of null in javascript for loop
body: <p>I'm trying to make a markdown editor like Stack Overflow has.</p>
  
  <p>I get this error listed in the title if I don't actually type an asterisk AND a http:// containing phrase into the textarea. If I only type an asterisk containing phrase, the error refers to this line: <code>if(linkify.length!==0)</code></p>
  
  <p>Here's a <a href="http://jsfiddle.net/trpeters1/2LAL4/47/" rel="nofollow">jsfiddle</a> to show you what I mean.</p>
  
  <p>Here's my HTML:</p>
  
  <pre><code>&lt;textarea&gt;&lt;/textarea&gt;
  &lt;div&gt;&lt;/div&gt;&lt;button type="button"&gt;Markdownify&lt;/button&gt;
  </code></pre>
  
  <p>Here's my JS:</p>
  
  <pre><code>var val=$('textarea').val(), boldify = val.match(/\*\*[A-Za-z0-9]+\*\*/gim),
  italicify = val.match(/\*[A-Za-z0-9]+\*/gim),linkify = val.match(/http\:\/\/[A-z0-9]+\.[A-z0-9]+/gim);
  
  $(document.body).on('click', 'button', function() {
  
  val=$('textarea').val(), boldify = val.match(/\*\*[A-Za-z0-9]+\*\*/gim),
  italicify = val.match(/\*[A-Za-z0-9]+\*/gim),linkify = val.match(/http\:\/\/[A-z0-9]+\.[A-z0-9]+/gim);
  
  if (boldify.length!== 0){
              for (var i=0; i &lt; boldify.length; i++)  {
                  var boldMatch= boldify[i],  
                  boldReplace = boldMatch.replace(/\*\*[A-z0-9]+\*\*/gim, '&lt;span style="font-weight:bold;color:blue;"&gt;'+boldMatch+'&lt;/span&gt;'),
                  val = val.replace(boldMatch, boldReplace);     
              }
          val = val.replace(/\*\*/gi, "");
  }
  
  if(italicify.length!== 0){
      for(var j=0; j &lt; italicify.length; j++){
      var italicMatch= italicify[j],
       italicReplace = italicMatch.replace(/\*[A-z0-9]+\*/gim, '&lt;span style="font-style:italic;color:red;"&gt;'+italicMatch+'&lt;/span&gt;');
      val = val.replace(italicMatch, italicReplace); 
      }
      val = val.replace(/\*/gi, "");
  }
  
  if(linkify.length!== 0){
      for(var k=0; k &lt; linkify.length; k++){
      var linkMatch= linkify[k],
      linkReplace = linkMatch.replace(/http:\/\/[A-z0-9]+\.[A-z0-9]+/gim, '&lt;a href="'+linkMatch+'"&gt;'+linkMatch+'&lt;/a&gt;');
          val = val.replace(linkMatch, linkReplace);
      }
  }
  
  $('div').html(val);
  });
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>