简体   繁体   English

如何从字符串获取url-jQuery或Javascript

[英]How to get url from string - jQuery or Javascript

i have url like this : 我有这样的网址:

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

i would like to know http://192.168.6.1 from those url...how can i achieve this using jquery or javascript? 我想从这些url知道http://192.168.6.1 ...如何使用jquery或javascript实现此目的?

what am i trying to do it : 我要做什么?

i got this string from my JavaScript :  http://192.168.6.1/Images/Work3ererg.png

using this javscript string : 使用这个javscript字符串:

i want to put **https://192.168.6.1/** instead of **http://localhost:37774** including http 我想放**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")

Thanks 谢谢

var url = 'http://192.168.6.1/Images/Work3ererg.png';
var host = url.substr(0,url.indexOf('/',7));

url.indexOf('/',7) means search / after http:// url.indexOf('/',7)表示在http://之后搜索/

Then use substr to get string from start to the first / after http:// 然后用substr得到的字符串从开始到第一/http://

If browser support (IE 10 and higher and recent browser), you could use the URL object . 如果支持浏览器(IE 10及更高版本和最新浏览器),则可以使用URL对象

You simply have to do that : 您只需要这样做:

var test = new URL('http://192.168.6.1/Images/regrgrger54654654.png')
console.log(test.origin)

If you want to use a regular expression, that would do it for this case : 如果要使用正则表达式,则可以在这种情况下使用它:

var url = 'http://192.168.6.1/Images/regrgrger54654654.png'

console.log(url.match(/https?:\/{2}[^\/]*/)[0]);

http://jsfiddle.net/8cd67Lzs/ http://jsfiddle.net/8cd67Lzs/

Extending the answer from: https://stackoverflow.com/a/736970/1026017 扩展答案来自: https : //stackoverflow.com/a/736970/1026017

var getHostname = function(href) {
    var l = document.createElement("a");
    l.href = href;
    return l.hostname;
};

Just replace part of string with another string: 只需将字符串的一部分替换为另一个字符串:

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);

you can use RegularExpression (pure JavaScript) to do this job for example you can use 您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM