patel
patel

Reputation: 635

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?

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 :

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

Upvotes: 0

Views: 74

Answers (5)

ismnoiet
ismnoiet

Reputation: 4159

you can use RegularExpression (pure JavaScript) to do this job for example you can use

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

Upvotes: 0

Slawomir Pasko
Slawomir Pasko

Reputation: 907

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

Upvotes: 0

Scott
Scott

Reputation: 3765

Extending the answer from: https://stackoverflow.com/a/736970/1026017

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

Upvotes: 0

Karl-André Gagnon
Karl-André Gagnon

Reputation: 33870

If browser support (IE 10 and higher and recent browser), you could use the URL object.

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/

Upvotes: 0

Chang
Chang

Reputation: 1413

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://

Then use substr to get string from start to the first / after http://

Upvotes: 3

Related Questions