Javascript jQuery在URL中提取页码

Javascript jQuery在URL中提取页码,javascript,jquery,regex,Javascript,Jquery,Regex,我需要使用正则表达式或字符串操作获取URL中最后出现的页码 www.somedomain.com/about us/meet the team/4/ www.somedomain.com/about us/meet the team/7/section/4/ www.somedomain.com/about us/meet the team/4/?val1=somevalue&val2=somevalue 我尝试过。替换(/\/\d\/)可能有更好的方法,但这里有一个: var str = 'w

我需要使用正则表达式或字符串操作获取URL中最后出现的页码

www.somedomain.com/about us/meet the team/4/

www.somedomain.com/about us/meet the team/7/section/4/

www.somedomain.com/about us/meet the team/4/?val1=somevalue&val2=somevalue


我尝试过
。替换(/\/\d\/)
可能有更好的方法,但这里有一个:

var str = 'www.somedomain.com/about-us/meet-the-team/7/section/4/?val1=somevalue&val2=somevalue'
var match = str.match(/\/(\d+)(?!.*\/\d+)/);
alert(match[1]);
正则表达式摘要:

  • \/(\d+)
    匹配
    /
    之后的数字(出现1次或多次),并将该数字保存到捕获组中
  • (?!.\/\d+)
    进行负向前瞻,以确保在任何字符后都不会再次匹配相同的模式


为什么所有的反对票都投了?为什么你什么都没试过?@nicael我试过很多东西,否则,我不会问这个问题,但我不知道为什么这很重要。我想我已经把问题说清楚了。虽然我看不到你的任何尝试,但效果很好。在本例中,是否有方法仅获取最后一次出现。www.somedomain.com/about us/meet the team/7/section/4/。那么,只有4个而不是7个被抓获了?