How to get the first string from the URL? - javascript

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.

Related

javascript - how to remove uri from a link

I got a link lie this
url = 'http//mysite.com/product/id/122?u=12'
I want to turn that to this
'http//mysite.com'
remove all the uri and query string
How you guys do it?
You can use URL api
let urlParsed = new URL("http://example.com/product/id/122?u=12")
let {origin} = urlParsed
console.log(origin)
Use split and string concatenation
var url = 'http//mysite.com/product/id/122?u=12'
console.log(url.split('.com/')[0]+'.com')

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

Best way to extract domain name in Jquery

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

Get specific part of url of a link

I want to get a specific part of a url between the third and fourth slashes of a link on the page.
EDIT: Sorry I don't think I was clear the first time, I meant getting the specific part of the url OF A LINK found on the page.
var getSegment = function (url, index) {
return url.replace(/^https?:\/\//, '').split('/')[index];
}
Usage:
getSegment("http://domain.com/a/b/c/d/e", 4); // "d"
The replace makes sure that the first two slashes after the protocol (http or https) don't count.
Here's a working example of getting a particular path segment.
Code:
var url = "www.test.com/one/two/three?p1=v&p2=v#anc";
var file = url.split('?')[0];
var pathanddomain = file.split('/');
var path = pathanddomain.splice(1, pathanddomain.length-1);
var pathIndexToGet = 2;
document.write(path[pathIndexToGet]);​
If you want to do this for the current page, use:
var url = window.location.href;
Also, if your url starts with http(s)://, you will need to remove this.
I'd suggest:
var link = 'http://www.example.com/directory1/directory2/directory3/directory4/index.html';
console.log(link.split('/')[5]);​
JS Fiddle demo.
The reason we're using [5] not [4] is because of the two slashes at the beginning of the URL, and because JavaScript arrays are zero-based.
you should elaborate you question and should specify which is your domain, that means on what purpose you are asking that question ??
This may help you:
var urlValue = url.split("/");
Then store urlValue as array.
then pick up third and forth value of the urlvalue on array.

Categories