2016-08-15 37 views
-4

我需要使用JavaScript來提取該URL的設備名稱:Javascript字符串操作 - 網址

http://store.myshop.com/my-shop-mobile/pay-monthly-phones/samsung-galaxy-j3-white/samsung-galaxy-j3-white-tariffs.html?filters=1

所以我只留下了 「三星銀河-J3白」

配件這個網址均是可變的,例如,它也可能是這樣的:

http://store.myshop.com/my-shop-mobile/pay-as-you-go-phones/galaxy-j3-white/samsung-galaxy-j3-white-tariffs.html?filters=1

但它是總是在域之後的第三個「/」之後。

+0

歡迎SO。請訪問[幫助]並參加[導覽],看看爲什麼您的問題被拒絕並可能關閉。提示:發佈相關代碼 – mplungjan

+0

我在這裏發現了另一個問題,其中提到刪除前綴,但只適用於第一部分:http://store.myshop.com/my-shop-mobile/ – megatron77

+1

提示:'str.split( 「/」)'http://www.w3schools.com/jsref/jsref_split.asp – cYrixmorten

回答

1

簡單的JavaScript數組方法,如splitreverse是很多解決這個問題的方法。

[ 
 
    'http://store.myshop.com/my-shop-mobile/pay-monthly-phones/samsung-galaxy-j3-white/samsung-galaxy-j3-white-tariffs.html?filters=1', 
 
    'http://store.myshop.com/my-shop-mobile/pay-as-you-go-phones/galaxy-j3-white/samsung-galaxy-j3-white-tariffs.html?filters=1' 
 
].forEach(function(a) { 
 
    console.log(a.split('/').reverse()[0].split('.html')[0]); 
 
});

+0

你爲什麼清除控制檯? –

+1

我通常在我的瀏覽器中寫我的答案代碼在控制檯中,所以它留在^^ 我會刪除它。 –