获取网址中的最后一个数字 [英] Get the last number in a URL

查看:229
本文介绍了获取网址中的最后一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到URL中从左到右的最后一个数字?

How do I find the last number in a URL, from left to right?

例如:

https://www.portal-gestao.com/introducao-6849.html

将返回:6849

并且:

https://www.portal-gestao.com/curso-modelos-negocio/1452-introducao/6850-melhores-praticas-no-uso-de-folhas-de-calculo.html

会返回:6850

这是我正在尝试的:

更新的代码

jQuery('a.title').each(function () {
    var $link=jQuery(this);
    var href=$link.attr('href'); 
    var idx = href.indexOf('/')!=-1?1:0; // choose the second one if slash
    var procura=href.match(/(\d+)/g)[idx];

    jQuery.each(obj,function(_,test) {
        if(test.indexOf(procura)!=-1) { // only works on strings 
            ....
        }
    });
});

推荐答案

这是一个通用的解决方案,可以为您提供字符串中的最后一个数字(不一定是URL)

Here's a general solution that would give you the last number in a string (not necessarily a URL)

function getLastNumberOfString(str){
  var allNumbers = str.replace(/[^0-9]/g, ' ').trim().split(/\s+/);
  return parseInt(allNumbers[allNumbers.length - 1], 10);
}

这篇关于获取网址中的最后一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆