gpt4 book ai didi

javascript - 获取url中特定的数字字符串

转载 作者:行者123 更新时间:2023-12-02 18:08:54 24 4
gpt4 key购买 nike

所以这里我们有一些来自同一域的网址:

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。

首先我获取 url,但是如何获取 id?

var url = $('a[href*="example"]');
var id = ???

最佳答案

使用正则表达式:

$('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
})

关于javascript - 获取url中特定的数字字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19872251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com