所以这里我们有一些来自同一个域的网址:
http://example.com/video6757788/sometext
http://example.com/video24353/someothertext
http://example.com/video243537786/somedifferenttext
http://example.com/video759882
http://example.com/video64353415
http://example.com/video342432?session=somestring
如何在所有类型的网址中获取视频后面的数字部分。我正在尝试获取视频ID。
首先我得到了网址,但是我怎么得到这个网址?
var url = $('a[href*="example"]');
var id = ???
答案 0 :(得分:2)
使用正则表达式:
$('a[href*="example"]').each(function() {
var $this = $(this);
var url = $this.attr("href");
var id = url.match(/video(\d+)/i)[1]; //retrieve the number following video*
//logic
})
或者,如果你想成为fancy with .attr(),则等同于:
$('a[href*="example"]').attr("href", function(indx, url) {
var id = url.match(/video(\d+)/i)[1]; //retrieve the number following video*
//logic
})