How to separate extra text after youtube id in youtube url using jquery?

时间:2018-02-05 12:53:14

标签: jquery youtube

This code is to check Youtube video link is valid or not but what I need is if user enter youtube Url in textarea and include some text using space or without space .so how that text should get separated after youtube id,and place in separate variable so please tell me where to change the code to get this.

<textarea name="youtube_url"></textarea>

    jQuery("textarea[name*='youtube_url']").blur(function () {
         var target = jQuery(this).val();

                          var regExp = /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
                          var match = target.match(regExp);
                          if (match && match[2].length == 11) {
                             var  videoID  = match[2];
                        }


                    $.ajax({
                        url: "https://gdata.youtube.com/feeds/api/videos/" + videoID + "?v=2&alt=json",
                        //dataType: "jsonp",
                        success: function(data) {
                            console.log(data)
                             // $("#result").text(data);
                        },
                        error: function(jqXHR, textStatus, errorThrown)
                                        {
                                            // Handle errors here
                                            alert('ERRORS: ' + textStatus);
                                        }
                    });
                        });

1 个答案:

答案 0 :(得分:0)

匹配网址

如果您想打破网址,您需要覆盖简短的网址并嵌入。

这是一个可行的正则表达式:

(http(s|):|)\/\/(www\.|)yout(.*?)\/(embed\/|watch.*?v=|)([a-z_A-Z0-9\-]{11})

以下是测试:Regex101

如果您需要身份证,则需要第6名会员。这是一个例子

&#13;
&#13;
    jQuery("textarea[name*='youtube_url']").blur(function() {
        var target = jQuery(this).val();

        var regExp = /(http(s|):|)\/\/(www\.|)yout(.*?)\/(embed\/|watch.*?v=|)([a-z_A-Z0-9\-]{11})/i;
        var match = target.match(regExp);
        // No need to check if 11 chars, already did that in the Regex
        var videoID = '';

        if (match[6]) {
            videoID = match[6];
        }

        console.log(videoID);
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">    </script>
<textarea name="youtube_url"></textarea>
&#13;
&#13;
&#13;

如果您想从视频中获取时间(如果它是有效的),您可以像这样改进正则表达式:

(http(s|):|)\/\/(www\.|)yout(.*?)\/(embed\/|watch.*?v=|)([a-z_A-Z0-9\-]{11})(\?t=.*|)

在第7组你会找到时间。如果您希望仅隔离数字,则会使用get参数?t=返回时间:

(http(s|):|)\/\/(www\.|)yout(.*?)\/(embed\/|watch.*?v=|)([a-z_A-Z0-9\-]{11})(?:\?t=|)(\d+|)

在网址后匹配。

因为我没有在第一时间收到问题,所以您可以通过以下方式获取网址后面的文字并获取网址(视频ID)。

(http(s|):|)\/\/(www\.|)yout(.*?)\/(embed\/|watch.*?v=|)([a-z_A-Z0-9\-]{11})(?:\?t=|)(\d+|)([\S\s]*)

([\S\s]*)将捕获任何内容,包括网址前后的换行符:

&#13;
&#13;
jQuery("textarea[name*='youtube_url']").blur(function() {
  var target = jQuery(this).val();

  var regExp = /(http(s|):|)\/\/(www\.|)yout(.*?)\/(embed\/|watch.*?v=|)([a-z_A-Z0-9\-]{11})(?:\?t=|)(\d+|)([\S\s]*)/i;
  var match = target.match(regExp);
  // No need to check if 11 chars, already did that in the Regex
  if (match[6]) {
    var videoID = match[6];
  }
  
  console.log("Video ID:");
  console.log(match[6]);
  console.log("Video start time:");
  console.log(match[7]);
  console.log("After:");
  console.log(match[8]);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p><strong>Example to try:</strong></p>
<p>https://youtu.be/xcJtL7QggTI?t=73 this a Sony 4k Video starts at 73s</p>
<textarea name="youtube_url"></textarea>
&#13;
&#13;
&#13;