0

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

2
  • 1
    possible duplicate of How do I parse a URL into hostname and path in javascript? Commented Oct 21, 2014 at 16:05
  • those are string as in my image source, i want to replce few image source using different image file name Commented Oct 21, 2014 at 16:09

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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/

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.