extract part of URL from parent URL - javascript

For this url
"http://testsite/sites/TestSubSite/objstrat/CultureConnection/Pages/default.aspx"
Trying to extract everything after enterprise/sites.
I want "/TestSubSite/objstrat/CultureConnection/Pages/default.aspx"
I can get the filename like this:
var filename = parenturl.substring(parenturl.lastIndexOf("/") + );
But not sure how to extract from the middle..

Using lastIndexOf will fail for any URI reference with / in the fragment or query, as in http://example.com/foo/bar#/baz/boo.
Libraries like Google Closure's Uri module can make this easy.
new goog.Uri(parenturl).getPath()
If your library of choice doesn't have URI support, then http://www.ietf.org/rfc/rfc3986.txt appendix B suggests
var urlRegex = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/;
var path = decodeURIComponent(urlRegex.exec(parenturl)[5]);
will get you the path part.
Once you've got the path, you can then strip off the /sites/ part by doing something like
var pathSuffix = path.replace(/^\/sites\//, '/');

I love Javascript with DOM element.
var link = document.createElement('a');
link.href = "http://testsite/sites/TestSubSite/objstrat/CultureConnection/Pages/default.aspx";
link.protocol;
link.hostname;
link.port;
link.pathname;
link.search;
link.hash;
link.host;
link.pathname.replace(/^\/sites/,"") // "/TestSubSite/objstrat/CultureConnection/Pages/default.aspx"

The easiest thing might be to use javascript's Split() function and rebuild the string from the resulting array (leaving out the first couple items)
var parenturl = "http://testsite/sites/TestSubSite/objstrat/CultureConnection/Pages/default.aspx";
var tokens = parenturl.split("/");
for(var i=0;i<tokens .length;i++)
{
console.log(tokens [i]);
}

Hows about:
var parenturl = "http://testsite/sites/TestSubSite/objstrat/CultureConnection/Pages/default.aspx";
var filename = parenturl.match(/\/sites(.*)$/)[1];
alert(filename);

Related

Get specific part from url origin with JS

I have to extract a specific part from an url origin. But I can't seem to figure out a good and fail proof way to do this
For example, this is my url:
http://site.domain.com
I need to know what "domain" is used. How do I get this part from the url 100% of the time?
My first thought was to split it between the "." but then I thought about what could happen if someone uses a http://www.site.domain.com link. That would mean it wouldn't work.
Further explanation:
I currently have 2 website which both have a differend domain name. I have to get the information about which page is loaded, based on the "domain" name in the url.
Thanks in regard!
split the full domain name on dot and than take element before last element.
var url = 'http://www.site.domain.com';
var array = url.split(".");
var lengthOfArray = array.length;
var domain = array[lengthOfArray-2];
console.log(domain);
Split by / using split("/"):
function getDomain(url){
var arr = url.split(".");
return arr[arr.length - 2];
}
console.log(getDomain('http://site.domain.com'));
console.log(getDomain('http://www.site.domain.com'));
You could split it by "." and get the next to last text of your string.
var str = 'http://www.site.domain.com';
var domain = str.split('.');
console.log(domain[domain.length-2]);
Later Edit
To provide further fail proof, you can get the latest text between .com and the first dot before it. This will work in case you have another dot in your URL, for example: 'http://www.site.domain.com/post/test.asp'.
var str = 'http://www.site.domain.com/post/test.asp';
var preComLink = str.substr(0, str.indexOf('.com'));
var preDomainIndex = preComLink.lastIndexOf('.');
var domain = preComLink.substr(preDomainIndex + 1, str.indexOf('com'));
console.log(domain);
let code = (function(){
return{
getDomain: function(url){
var slpitDomainURL = url.split(".");
var lengthOfArray = slpitDomainURL.length;
var domain = slpitDomainURL[lengthOfArray-2];
return domain;
}
}
})();
console.log(code.getDomain('http://www.google.com'));
console.log(code.getDomain('http://www.facebook.com'));

How to strip / replace something from a URL?

I have this URL
http://192.168.22.124:3000/temp/box/c939c38adcf1873299837894214a35eb
I want to replace the last part of my URL which is c939c38adcf1873299837894214a35eb with something else.
How can I do it?
Try this:
var url = 'http://192.168.22.124:3000/temp/box/c939c38adcf1873299837894214a35eb';
somethingelse = 'newhash';
var newUrl = url.substr(0, url.lastIndexOf('/') + 1) + somethingelse;
Note, using the built-in substr and lastIndexOf is far quicker and uses less memory than splitting out the component parts to an Array or using a regular expression.
You can follow this steps:
split the URL with /
replace the last item of array
join the result array using /
var url = 'http://192.168.22.124:3000/temp/box/c939c38adcf1873299837894214a35eb';
var res = url.split('/');
res[res.length-1] = 'someValue';
res = res.join('/');
console.log(res);
Using replace we can try:
var url = "http://192.168.22.124:3000/temp/box/c939c38adcf1873299837894214a35eb";
var replacement = 'blah';
url = url.replace(/(http.*\/).*/, "$1" + replacement);
console.log(url);
We capture everything up to and including the final path separator, then replace with that captured fragment and the new replacement.
Complete guide:
// url
var urlAsString = window.location.href;
// split into route parts
var urlAsPathArray = urlAsString.split("/");
// create a new value
var newValue = "routeValue";
// EITHER update the last parameter
urlAsPathArray[urlAsPathArray.length - 1] = newValue;
// OR replace the last parameter
urlAsPathArray.pop();
urlAsPathArray.push(newValue);
// join the array with the slashes
var newUrl = urlAsPathArray.join("/");
// log
console.log(newUrl);
// output
// http://192.168.22.124:3000/temp/box/routeValue
You could use a regular expression like this:
let newUrl = /^.*\//.exec(origUrl)[0] + 'new_ending';

Removing https:// and .com from String

I am trying to convert a group of given URLs to another format I'll need in another part of the application.
So for the string https://www.myUrl.com/research/case-studies I need the following output:
myUrl/research/case studies
The following function I created manages just fine but I cannot help feeling I'm not being efficient enough by doing everything in just one line.
function generate_conversion(convert_input, url_format){
simple_urls = [];
var new_url = '';
$.each(convert_input, function(index, item) {
new_url = item.replace(/^(https?:\/\/)?(www\.)?(?:\.[a-z\.]+[\/]?)?/,'');
new_url = new_url.replace(/([.]\w+)$/, '');
new_url = new_url.replace(/.com/g, '');
simple_urls.push(new_url.replace(/-/g, ' '));
});
console.log(simple_urls);
}
Is there a more efficient or readable way to achieve the same result? I cannot use window.location.host or similar methods because the URLs are passed as simple strings.
depends on my understanding try this.,
var originalString="https://www.myUrl.com/research/case-studies";
var replacedString=originalString.replace("https://www.","")
console.log(replacedString);
Just do it
var str = "https://www.myUrl.com/research/case-studies";
var simple_url = str.substring(str.indexOf("www.")+4);
console.log(simple_url);

How can I extract a part of this url with JavaScript?

I have an url that looks like this:
http://localhost/assets/upload/img/hw6dNDBT-36x36.jpg
I want to extract hw6dNDBT.jpg, from the url above.
I tried playing around with regex patterns /img\/.*-/ but that
matches with img/hw6dNDBT-.
How can I do this in JavaScript?
try this:
var url = 'http://localhost/assets/upload/img/hw6dNDBT-36x36.jpg';
var filename = url.match(/img\/(.*)-[^.]+(\.[^.]+)/).slice(1).join('');
document.body.innerHTML = filename;
i would use split() method:
var str = "http://localhost/assets/upload/img/hw6dNDBT-36x36.jpg";
var strArr = str.split("/");
var size = strArr.length - 1;
var needle = strArr[size].split("-");
var fileTypeArr = strArr[size].split(".");
var name = needle[0]+"."+fileTypeArr[fileTypeArr.length-1];
name should now be your searched String so far it contains no / inside it
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
/[^\/]+$/ should match all characters after the last / in the URL, which seems to be what you want to match.
No regex:
//this is a hack that lets the anchor tag do some parsing for you
var parser = document.createElement('a');
parser.href = 'http://localhost/assets/upload/img/hw6dNDBT-36x36.jpg';
//optional if you know you can always trim the start of the path
var path = parser.pathname.replace('/assets/uploads/');
var parts = path.split('/');
var img = '';
for(var i=0; i<parts.length; i++) {
if (parts[i] == 'img') {
//since we know the .jpg always follows 'img/'
img = parts[i+1];
}
}
Ah, you were so close! You just need to take your regex and use a capturing group, and then add a littttle bit more!
img\/(.*)-.*(\..*)
So, you can use that in this manner:
var result = /img\/(.*)-.*(\..*)/.exec();
var filename = result[1] + result[2];
Honestly capturing the .jpg, is a little excessive, if you know they are all going to be JPG images, you can probably just take out the second half of the regex.
Incase you are wondering, why do we uses result[1] and result[2]? Because result[0] stores the entire match, which is what you were getting back. The captured groups, which is what we create when we use the parentheses, are stored as the indexes after 0.
Here is some one-liner:
var myUrl = 'http://localhost/assets/upload/img/hw6dNDBT-36x36.jpg',
myValue = myUrl.split('/').pop().replace(/-(?=\d).[^.]+/,'');
We take everything after the last slash then cut out the dimension part.

How to strip all parameters and the domain name from a URL using javascript?

Given a series of URLs
http://www.anydotcom.com/myfolder/some-url.html
http://www.anydotcom.com/myfolder2/index.html#
http://www.anydotcom.com/myfolder3/index.html?someParam=aValue
http://www.anydotcom.com/foldername/index.html?someParam=anotherValue
First, how could I strip anything off the end of the URL so that I end up with
http://www.anydotcom.com/myfolder/some-url.html
http://www.anydotcom.com/myfolder2/index.html
http://www.anydotcom.com/myfolder3/index.html
http://www.anydotcom.com/foldername/index.html
or, ideally, I would like it to return
/myfolder/some-url.html
/myfolder2/index.html
/myfolder3/index.html
/foldername/index.html
I've tried
var thisUrl = "" + window.location;
var myRegExp = new RegExp("([^(\?#)]*)");
thisUrl = myRegExp.exec(thisUrl);
but this returns
http://www.anydotcom.com/foldername/index.html,http://www.anydotcom.com/foldername/index.html
and I don't quite understand why.
I appreciate any help here!
Well, to answer your question directly, here's the regular expression to do that.
thisUrl = thisUrl.replace( /^https?:\/\/[^\/]|\?.*$/g, '' );
However, since you mention window.location in your code, you can actually get this data straight from the location object.
thisUrl = top.location.pathname;
If you are using window.location, you can simply access the wanted data by using:
var thisUrl = window.location.pathname;
If you are extracting stuff from links, the following regular expression will get you what you need:
// Supports all protocols (file, ftp, http, https, whatever)
var pathExtract = /^[a-z]+:\/\/\/?[^\/]+(\/[^?]*)/i;
var thisUrl = (pathExtract.exec(someUrl))[1];
Javascript location object
var loc = window.location;
var thisUrl = loc.protocol + "//" + loc.hostname + loc.pathname;
using the object window.location is simple as write:
function getPath() {
return window.location.pathname;
}

Categories