2013-11-20 62 views
0

以下腳本將檢索用戶所在的當前頁面的URL。僅從變量值檢索整數

$(location).attr('href'); 
    //pure javascript 
    var pathname = window.location.pathname; 
    // to show it in an alert window 
    alert(window.location); 

例如,它會檢索以下對我來說:

http://domain.com/Men/000101,en_US,sc.html?src=sale

我會再像只檢索該網址中顯示的數字,所以我會用000101只落得和保存在另一個變量中。問題是每個網址都有不同的大小號碼。這可能嗎?

謝謝!

+0

是否可以使用正則表達式?...發佈一些網址,看看如何創建,請。 – Cyrus

回答

0

嘗試

var num = pathname.split('/').pop().split(',')[0] 
0

使用正則表達式來剝奪一切,但數字:

var pathName = window.location.pathname; 
var digitsInPathName = pathName.replace(/\D/g, ''); 

注意這是一個天真的解決方案,如果有數字的其他任何地方的網址,例如將打破作爲域或查詢字符串。