Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a URL?

If there is one thing I just cant get my head around, it's regex.

So after a lot of searching I finally found this one that suits my needs:

function get_domain_name()     {      aaaa="http://www.somesite.se/blah/sdgsdgsdgs";     //aaaa="http://somesite.se/blah/sese";         domain_name_parts = aaaa.match(/:\/\/(.[^/]+)/)[1].split('.');         if(domain_name_parts.length >= 3){             domain_name_parts[0] = '';         }         var domain = domain_name_parts.join('.');         if(domain.indexOf('.') == 0)             alert("1"+ domain.substr(1));         else             alert("2"+ domain);     } 

It basically gives me back the domain name, is there anyway I can also get all the stuff after the domain name? in this case it would be /blah/sdgsdgsdgs from the aaaa variable.

like image 377
Ryan Avatar asked May 29 '11 14:05

Ryan


People also ask

How do you parse a URL?

The url. parse() method takes a URL string, parses it, and it will return a URL object with each part of the address as properties. Parameters: This method accepts three parameters as mentioned above and described below: urlString: It holds the URL string which needs to parse.

What is parsing of URL?

URL parsing is a function of traffic management and load-balancing products that scan URLs to determine how to forward traffic across different links or into different servers. A URL includes a protocol identifier (http, for Web traffic) and a resource name, such as www.microsoft.com.


1 Answers

EDIT (2020): In modern browsers, you can use the built-in URL Web API.

https://developer.mozilla.org/en-US/docs/Web/API/URL/URL

var url = new URL("http://www.somesite.se/blah/sdgsdgsdgs"); var pathname = url.pathname; // returns /blah/sdgsdgsdgs 

Instead of relying on a potentially unreliable* regex, you should instead use the built-in URL parser that the JavaScript DOM API provides:

var url = document.createElement('a'); url.href = "http://www.example.com/some/path?name=value#anchor"; 

That's all you need to do to parse the URL. Everything else is just accessing the parsed values:

url.protocol; //(http:) url.hostname; //(www.example.com) url.pathname; //(/some/path) url.search; // (?name=value) url.hash; //(#anchor) 

In this case, if you're looking for /blah/sdgsdgsdgs, you'd access it with url.pathname

Basically, you're just creating a link (technically, anchor element) in JavaScript, and then you can make calls to the parsed pieces directly. (Since you're not adding it to the DOM, it doesn't add any invisible links anywhere.) It's accessed in the same way that values on the location object are.

(Inspired by this wonderful answer.)

EDIT: An important note: it appears that Internet Explorer has a bug where it omits the leading slash on the pathname attribute on objects like this. You could normalize it by doing something like:

 url.pathname = url.pathname.replace(/(^\/?)/,"/"); 

Note: *: I say "potentially unreliable", since it can be tempting to try to build or find an all-encompassing URL parser, but there are many, many conditions, edge cases and forgiving parsing techniques that might not be considered or properly supported; browsers are probably best at implementing (since parsing URLs is critical to their proper operation) this logic, so we should keep it simple and leave it to them.

like image 99
Yahel Avatar answered Oct 05 '22 14:10

Yahel