title: Get YouTube video id from URL with jQuery
body: <p>I'm trying to take a textarea input that takes any YouTube URL as the input and embed the video much like Facebook does. </p>
  
  <p>I have:</p>
  
  <pre><code>var text = $('#content').val().split(' ');
      for (i = 0; i &lt; text.length; i++) {
          var test = text[i].indexOf('youtube.com/watch');
          var check = text[i].indexOf('[youtube=');
  
          if (test != -1 &amp;&amp; check == -1) {
              var ytid = text[i].substring(text[i].lastIndexOf('=') + 1);
              text[i] = '[youtube=' + ytid + ']';
          }
      }
  var boxval = text.join(' ').trim();
  </code></pre>
  
  <p>which takes any YouTube URL and makes it into a <code>[youtube=videoid]</code>. Problem then is that when a URL is submitted with a <code>&lt;br&gt;</code> or a <code>\n</code> at the end it adds the <code>]</code> afterwards. </p>
  
  <p>Anyone know of a better way?</p>