Best way to extract domain name in Jquery - javascript

I have an URL e.g http://test.example.com I need to extract test (sub domain of this url.) from this url. This url is dynamic it may change so couldn't match with test and extract it. I have written block of code it is working for me. Can anyone suggest me better way to achieve this.
var siteUrl = 'http://test.example.com';
var parts = siteUrl.split('.');
var subdomainstr = parts.shift(); // Output 'http://test'
var upperleveldomain = parts.join('.'); // Output 'example.com'
var extractSubDomain = subdomainstr.split('//');
var subdomain = extractSubDomain.slice(1).join('.');
console.log(subdomain); //Output test
fiddle

Why not this?
var subdomain = siteUrl.split('//')[1].split('.')[0];
Working demo

If you prefer regex, this works
var subdomain = siteUrl.match(/http:\/\/([^\.]*)/i)[1];

Related

How to get the first string from the URL?

I'm a newbie in web development so pls forgive my newbie question.
I have a URL "https://123asd.my.website.com/blabla/blabla/blabla
What I'm trying to figure out is how do I get the "123asd" so that I can set in on my var. Thank you
You can use regex
var url = 'https://123asd.my.website.com/blabla/blabla/blabla';
var number = url.match(/([0-9a-z]{1,})\./)[1];
console.log(number);
const url = "https://123asd.my.website.com/blabla/blabla/blabla";
let firstStr = url.replace("https://", ""); // get rid of "https://" or you can do it by some other way
firstStr = firstStr.substring(0, firstStr.indexOf('.')); // get the substring start from the beginning to the first '.'
console.log(firstStr); // 123asd
var url="https://123asd.my.website.com/blabla/blabla/blabla";
var urlNoHttps=url.replace(/^https?\:\/\//i, "");
var hostName=urlNoHttps.split('.')[0];
console.log(hostName);
The above code works for both http and https protocol.

Get the url Parameter string from Javascript

I need the whole parameter list as such , not one by one
var Url = "http://localhost/Home/Admin?param1=1&param2=2$param3=3";
I want to get the whole parameter list from the url.
var params = "param1=1&param2=2&param3=3";
var Url = "http://localhost/Home/Admin?param1=1&param2=2$param3=3";
var urlArray = url.split("?");
var params=urlArray[1];
You can see Using split() example of Mozilla Developer Network for more insight on using the split function.
Thanks for the support, I use this one for my need
var params = window.location.href.split('?')[1];

How to find current page url (like "index.html")?

With string http://www.example.com/section_one/index.html how can I return index using RegExp?
P.S.: Returning index.html is accepted too.
You don't need to use regex for that. Its simple:
var url = document.URL;
var currentPage= url.split("/").pop();
You can try the following.
Fiddle
var url = "http://www.example.com/section_one/index.html";
var filename = url.match(/[^\\/]+$/)[0];

javascript/jquery grab part of URL

I have for example, https://www.example.com/test1/something?asd=1. Of this example URL I need to grab everything up until and included /test1/. So I would set var url = https://www.example.com/test1. The problem is that test1 is dynamic so I can not have any hard coded values.
How can I do this?
One way is to use a combination of split() and join():
var url = "https://www.example.com/test1/something?asd=1";
var result = url.split("/",4).join("/");
Here's a JSFiddle of it in action:
http://jsfiddle.net/msm3jsvw/

Can I pass multiple args (patterns) for Javascript's replace()? If not, any good alternative?

I'm taking the following ex URL https://support.dev.mysite.com/batch/ and removing everything, but the environment (eg dev). The below code works fine.
var env = endPoint.replace("https://support.", "\n");
var envClean = env.replace(".mysite.com/batch/", "\n");
I don't like repeating myself. I would like to look for both patterns in the string and remove them all at once. MDN has a good breakdown of replace() here - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/replace but it doesn't mention anything about multiple arguments.
I've tried this:
var env = endPoint.replace("https://support." && ".mysite.com/batch/", "\n");
but it just parses the second arg and disregards the first.
Does anyone have a cleaner way of doing this? I'm assuming I can search for multiple patterns via REGEX, any REGEX masters out there care to help?
Cheers.
You can use regular expressions for this:
var environment = 'https://support.dev.mysite.com/batch/'
.replace(/^https:\/\/support\.|\.mysite\.com\/batch\/$/g, '');
You could chain your method:
var envClean = endPoint.replace("https://support.", "\n").replace(".mysite.com/batch/", "\n");
Or you could use regex:
var envClean = endPoint.replace(/https:\/\/support\.|\.mysite\.com\/batch\//, "\n");
And there is another solution to get dev:
var envClean = endPoint.match(/^https:\/\/support\.([^.]*)\.mysite\.com\/batch\/$/)[1];
For this specific URL pattern, why not make it really simple and use .split():
var url = 'https://support.dev.mysite.com/batch/';
var name = url.split('.')[1];
If I were using a regular expression, I would probably do it this way:
var match = url.match( /support\.(.*)\.mysite.com/ );
var name = match && match[1];
Note that you don't have to worry about the entire URL this way, only enough to do the match.
If you know that the URL will match, you can simplify that to:
var name = url.match( /support\.(.*)\.mysite.com/ )[1];

Categories