从当前url javascript获取字符串斜杠后的值
我的当前页面url如下所示:从当前url javascript获取字符串斜杠后的值,javascript,regex,url,Javascript,Regex,Url,我的当前页面url如下所示: http://localhost/admin/namespace_module/yeezy/index/test_id/5/key/23123asda/ url可以是动态的,但始终包含/test\u id/value/,首先我想检查当前url中是否存在/test\u id/: window.location.href 然后我需要使用RegExp\test方法检索值 if(/\/test_id\//.test(window.location.href)){ }
http://localhost/admin/namespace_module/yeezy/index/test_id/5/key/23123asda/
url可以是动态的,但始终包含/test\u id/value/,首先我想检查当前url中是否存在/test\u id/:
window.location.href
然后我需要使用RegExp\test
方法检索值
if(/\/test_id\//.test(window.location.href)){
}
if(window.location.href.indexOf('/test_id/') > -1){
}
或者使用String#indexOf
方法
if(/\/test_id\//.test(window.location.href)){
}
if(window.location.href.indexOf('/test_id/') > -1){
}
使用RegExp#test
方法
if(/\/test_id\//.test(window.location.href)){
}
if(window.location.href.indexOf('/test_id/') > -1){
}
或者使用String#indexOf
方法
if(/\/test_id\//.test(window.location.href)){
}
if(window.location.href.indexOf('/test_id/') > -1){
}
您可以使用正则表达式结合使用
.test()
进行检查,并使用.match()
提取数字:
var url=”http://localhost/admin/namespace_module/yeezy/index/test_id/5/key/23123asda/"; // window.locatioin.href;
如果(/test_id\/[0-9]+/.test(url)){
console.log(url.match(/test\u id\/[0-9]+/)[0]。match(/[0-9]+/)[0]);
//--测试id/5----------------^^^^^^^^^^^^^----5--^^^^^^^^^^^^
}//--output------------------
您可以使用正则表达式结合使用.test()
进行检查,然后使用.match()
提取数字:
var url=”http://localhost/admin/namespace_module/yeezy/index/test_id/5/key/23123asda/"; // window.locatioin.href;
如果(/test_id\/[0-9]+/.test(url)){
console.log(url.match(/test\u id\/[0-9]+/)[0]。match(/[0-9]+/)[0]);
//--测试id/5----------------^^^^^^^^^^^^^----5--^^^^^^^^^^^^
}//--输出------------------输出-----------------
要在测试模式后获取数字,请执行以下操作:
var match = window.location.href.match(/\/test_id\/(\d+)/);
if (match) {
// pattern is OK, get the number
var num = +match[1];
// ...
}
要在测试模式后获取数字,请执行以下操作:
var match = window.location.href.match(/\/test_id\/(\d+)/);
if (match) {
// pattern is OK, get the number
var num = +match[1];
// ...
}
那么如何检索斜杠后的值?@IdhamChoudry:
window.location.href.split('/test\u id/').pop()
那么如何检索斜杠后的值?@IdhamChoudry:window.location.href.split('/test\u id/').pop()
此方法的缺点是效率不高。一个正则表达式最多执行三次,而一次正则表达式执行就可以完成。这种方法的缺点是效率不高。一个正则表达式最多执行三次,而一次正则表达式执行即可完成。