如果URL包含字符串,则使用子字符串

时间:2016-03-09 02:47:24

标签: jquery

我想写一个jQuery函数,它读取类似字符串的URL,然后用它后的子字符串做一些事情。像这样:

if (window.location.href.indexOf('/more/**ID_NUMBER**') > -1) {
    console.log('got more');
    console.log(this);
    console.log(**ID_NUMBER**);
}

但我不确定如何在没有硬编码的情况下引用**ID_NUMBER**。有什么帮助吗?

1 个答案:

答案 0 :(得分:3)

您正在寻找正则表达式:

var id = /\/more\/(\d+)/.exec(location.href);
if (id)
相关问题