[英]How to get url from string - jQuery or Javascript
我有這樣的網址:
http://192.168.6.1/Images/Work3ererg.png
http://192.168.6.1/Images/WorwefewfewfefewfwekThumb.png
http://192.168.6.1/Images/ewfefewfewfewf23243.png
http://192.168.6.1/Images/freferfer455ggg.png
http://192.168.6.1/Images/regrgrger54654654.png
我想從這些url知道http://192.168.6.1
...如何使用jquery或javascript實現此目的?
我要做什么?
i got this string from my JavaScript : http://192.168.6.1/Images/Work3ererg.png
使用這個javscript字符串:
我想放**https://192.168.6.1/**
而不是**http://localhost:37774**
包括http
$("#" + id).css("background", "rgba(0, 0, 0, 0) url(http://localhost:37774/Images/PBexVerticalLine1.png) no-repeat scroll 0% 0% / auto padding-box border-box")
謝謝
var url = 'http://192.168.6.1/Images/Work3ererg.png';
var host = url.substr(0,url.indexOf('/',7));
url.indexOf('/',7)
表示在http://
之后搜索/
然后用substr
得到的字符串從開始到第一/
后http://
如果支持瀏覽器(IE 10及更高版本和最新瀏覽器),則可以使用URL對象 。
您只需要這樣做:
var test = new URL('http://192.168.6.1/Images/regrgrger54654654.png')
console.log(test.origin)
如果要使用正則表達式,則可以在這種情況下使用它:
var url = 'http://192.168.6.1/Images/regrgrger54654654.png'
console.log(url.match(/https?:\/{2}[^\/]*/)[0]);
擴展答案來自: https : //stackoverflow.com/a/736970/1026017
var getHostname = function(href) {
var l = document.createElement("a");
l.href = href;
return l.hostname;
};
只需將字符串的一部分替換為另一個字符串:
var originalString = "http://192.168.6.1/Images/freferfer455ggg.png";
var newString = originalString.replace("http://192.168.6.1/","https://192.168.6.1/");
console.log(newString);
您可以使用RegularExpression (純JavaScript)來完成此工作,例如,您可以使用
var ip = ''; // will contain the ip address
var ips = [] // ips is an array that will contain all the ip address
var url = 'http://192.168.6.1/Images/Work3ererg.png';
url.replace(/http:\/\/(.+?)\//,function(all,first){
// first will be something like 192.168.6.1
// while all will be something like http://192.168.6.1
ip = first;
});
// url can be a a list of ip address in this case we should add the
// g flag(which means global, not just the first match but all the matches )
url ='http://192.168.6.1/Images/Work3ererg.png';
url +='http://192.168.6.2/Images/Work3ererg.png';
url.replace(/http:\/\/(.+?)\//g,function(all,first){
ips.push(first);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.