2016-02-02 88 views
1

我有一組網址,需要獲取特定部分。 URL的格式爲:獲取特定部分的網址

http:\/\/xxx.xxxxx.com\/xxxx\/xxxx\/1234567_1.jpg 

我需要得到1234567位和存儲在一個變種。

+2

你有什麼企圖?任何REGEX? – Pogrindis

回答

1

你應該可以通過使用函數檢查URL來使它與你的JSON字符串一起工作。像這樣的東西應該工作:

function checkForMatches(str) { 
    var res = str.match(/.*\/(.*)_1.jpg/);  
    if(res) { 
     output = res[res.length-1]; 
    } else { 
     output = false; 
    }  
    return output;  
} 


$.get("test.php", function (data) { 
    // now you can work with `data` 
    var JSON = jQuery.parseJSON(data); // it will be an object 
    $.each(JSON.deals.items, function (index, value) { 
     //console.log(value.title + ' ' + value.description); 
     tr = $('<tr/>'); 
     tr.append("<td>" + "<img class='dealimg' src='" + value.deal_image + "' >" + "</td>"); 
     tr.append("<td>" + "<h3>" + value.title + "</h3>" + "<p>" + value.description + "</p>" + "</td>"); 
     //tr.append("<td>" + value.description + "</td>"); 
     tr.append("<td> £" + value.price + "</td>"); 
     tr.append("<td class='temperature'>" + value.temperature + "</td>"); 
     tr.append("<td>" + "<a href='" + value.deal_link + "' target='_blank'>" + "View Deal</a>" + "</td>"); 

     myvar = checkForMatches(value.deal_link); 
     if(myvar == false) { 
      myvar = value.deal_link; //if no matches, use the full link 
     } 


     tr.append("<td>" + "<a href='" + myvar + "' target='_blank'>" + "Go To Argos</a>" + "</td>"); 
     $('table').append(tr); 
    }); 
}); 

 
 

更早,更基本的例子。

您可以使用正則表達式來查找匹配項。

像這樣的東西會工作:

var str = "http:\/\/xxx.xxxxx.com\/xxxx\/xxxx\/1234567_1.jpg"; 
var res = str.match(/.*\/(.*)_1.jpg/); 
alert(res[1]) 

如果你想去遠一點有了它,你可以創建一個函數,並傳遞你想測試字符串,它會返回匹配的值,如果如果不存在匹配,則返回boolean false。

像這樣的工作:

function checkForMatches(str) { 
    var res = str.match(/.*\/(.*)_1.jpg/); 

    if(res) { 
     output = res[res.length-1]; 
    } else { 
     output = false; 
    } 

    return output; 

} 


alert(checkForMatches("http:\/\/xxx.xxxxx.com\/xxxx\/xxxx\/1234567_1.jpg")) 
alert(checkForMatches("this is an invalid string")) 

你可以看到它在這裏工作:https://jsfiddle.net/9k5m7cg0/2/

希望幫助!

+0

如果我嘗試並將其存儲在var它給了我一個無效的錯誤 - var res2 = res [1]; – JohnyChew

+0

你使用什麼代碼和輸入字符串?將它設置爲另一個變量應該沒有問題,你可以在這裏看到:https://jsfiddle.net/9k5m7cg0/1/ –

+0

這可以在jQuery中完成嗎? – JohnyChew

5

那麼你可以做劈叉

"http://xxx.xxxxx.com/xxxx/xxxx/1234567_1.jpg".split("/").pop().split("_").shift() 

或正則表達式

"http://xxx.xxxxx.com/xxxx/xxxx/1234567_1.jpg".match(/\/(\d+)_\d+\.jpg$/).pop() 
0

這裏的想法:採取最終/字符後到來的一切,然後附帶子字符串範圍內採取一切在第一個_字符之前。

var getUrlTerm = function(url) { 
    var urlPcs = url.split('/'); 
    var lastUrlPc = urlPcs[urlPcs.length - 1]; 
    return lastUrlPc.split('_')[0]; 
} 
1
var pathArray = window.location.pathname.split('/'); 

分裂1/2/3/4的...

因此,要獲得路徑2這將是:

var setLocation = pathArray[1]; 
0

您都可以鏈接到「 '元素和使用JavaScript內置的方法,讓您的生活更輕鬆:

var parser = document.createElement('a'); 
parser.href = "YOUR URL HERE"; 
var fileName = parser.pathname.split('/').pop(); 
var code = fileName.split('_')[0]; 

code將會有你想要的值。

1

那麼這應該做

function getLastFolder(){ 
    var path = window.location.href; 
    var folders =path.split("/"); 
    return folders[folders.length-1]); 
} 
0

我會使用一個正則表達式,並感覺到它好像你正在尋找的數字,你可以做的是,正則表達式過濾器。

var path = window.location.pathname, 
     regFilter = /\d*/g, 
     filter = regFilter.exec(path); 

正則表達式\ d縮小您的過濾搜索僅查找的數字。 *抓住一組數字。

你的結果是在過濾器變種。唯一的一點是,exec會返回一個包含原始字符串和返回結果的數組,並將返回1索引,因此您必須像這樣從中抓取它。

filter[1];