如果我有如下的地址欄URL, https://subdomain.mysite.com/abc/xyz/MYID9878797878/page/#/route1/step1/step2從地址欄中提取字符串URL
從此URL字符串獲取「MYID9878797878」的最佳方法是什麼?
我應該使用正則表達式嗎?
如果我有如下的地址欄URL, https://subdomain.mysite.com/abc/xyz/MYID9878797878/page/#/route1/step1/step2從地址欄中提取字符串URL
從此URL字符串獲取「MYID9878797878」的最佳方法是什麼?
我應該使用正則表達式嗎?
好吧,如果這是在路徑的同一部分,你可以這樣做:
var mystr = url.split('/')[5];
如果它始終是這個數字的/
在此之後將工作的console.log(location.href.split( '/')[5]);
試試這個
:
document.location.pathname.split('/')[3]
var url = "https://subdomain.mysite.com/abc/xyz/MYID9878797878/page/#/route1/step1/step2";
var parameter3 = url.split('/')[5];
//OR var parameter3 = location.href.split('/')[5];
// console.log(parameter3) or alert(parameter3)
是否有可能不使用拆分,即不依賴索引?我知道它始終以MYID – testndtv
開頭,那麼請嘗試JavaScript String match()方法。 –
示例代碼var maches = url.match(/ MYID(。*)\/page /); –
有這麼多的聲譽來承擔這麼多的責任。 – guradio
@guradio我很驚訝,因爲你! –