title: Grab YouTube ID and additional variables for embedding purposes with JavaScript
body: <p>So I've seen lots of scripts to grab the YouTube ID from a URL with JavaScript, but I can't find or figure out how to grab the ID along with any additional variables. I have a PHP script which can do it, but I'd like to do this with JavaScript. Does anyone know how this can be accomplished?</p>
  
  <p>I doubt it's necessary but here are the two scripts I'm using</p>
  
  <p>JavaScript for video ID...</p>
  
  <pre><code>url = $(this).text();
  var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&amp;\?]*).*/;
  var match = url.match(regExp);
  if (match&amp;&amp;match[2].length==11){
      alert(match[2]);
  }else{
     alert("not youtube");
  }
  </code></pre>
  
  <p>And PHP...</p>
  
  <pre><code>if (preg_match('%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&amp;]v=)|youtu\.be/)([^"&amp;?/ ]{11})%i', $url, $match)) {
  $youtubeid = $match[1];
  $getyt = (parse_url($url));
  }
  if(strlen($getyt['fragment']) &gt; 0) {
  $addhash = '#' . $getyt['fragment'];
  }
  $embed = "http://www.youtube.com/embed/$youtubeid?wmode=opaque&amp;autoplay=1$addhash";
  </code></pre>
  
  <p>The PHP one is pretty good, except that it only works for hash tags. My primary reason for wanting the additional variables is for when someone wants to specify a start time. So if anyone can tell me how I can grab the URL and additional paramaters (or just the specified starting time of the video) then I'd really appreciate it, because I'm absolutely lost.</p>