views:

3080

answers:

5

I need to write some javascript to strip the hostname:port part from a url, meaning I want to extract the path part only.

i.e. I want to write a function getPath(url) such that getPath("http://host:8081/path/to/something") returns "/path/to/something"

Can this be done using regular expressions?

+7  A: 

Quick 'n' dirty:

^.*?://.*?(/.*)$

Everything after the hostname and port (including the initial /) is captured in the first group.

strager
Or in regular expression literal form ("/" needs to be escaped): /^.*?:\/\/.*?(\/.*)$/.exec("http://example.com/folder/file.ext")[1] gives "/folder/file.ext"
Ates Goral
This regex is wrong. It captures the path, query and fragment in group 1.
Mike Samuel
Regex isn't necessary at all! Nice though!
J-P
@mikesamuel, The question asked to remove the hostname and port. I'll correct my answer to have a suitable explanation, though.
strager
A: 

This regular expression seems to work: (http://[^/])(/.)

As a test I ran this search and replace in a text editor:

 Search: (http://[^/]*)(/.*)
Replace: Part #1: \1\nPart #2: \2

It converted this this text:

http://host:8081/path/to/something

into this:

Part #1: http://host:8081
Part #2: /path/to/something

and converted this:

http://stackoverflow.com/questions/441755/regular-expression-to-remove-hostname-and-port-from-url

into this:

Part #1: http://stackoverflow.com
Part #2: /questions/441755/regular-expression-to-remove-hostname-and-port-from-url
jussij
You might want to use https?:// so that https:// URLs work as well.
Graeme Perrow
+7  A: 

RFC 3986 ( http://www.ietf.org/rfc/rfc3986.txt ) says in Appendix B

The following line is the regular expression for breaking-down a well-formed URI reference into its components.

  **^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?**
   12            3  4          5       6  7        8 9

The numbers in the second line above are only to assist readability; they indicate the reference points for each subexpression (i.e., each paired parenthesis). We refer to the value matched for subexpression as $. For example, matching the above expression to

  http://www.ics.uci.edu/pub/ietf/uri/#Related

results in the following subexpression matches:

  $1 = http:
  $2 = http
  $3 = //www.ics.uci.edu
  $4 = www.ics.uci.edu
  $5 = /pub/ietf/uri/
  $6 = <undefined>
  $7 = <undefined>
  $8 = #Related
  $9 = Related

where indicates that the component is not present, as is the case for the query component in the above example. Therefore, we can determine the value of the five components as

  scheme    = $2
  authority = $4
  path      = $5
  query     = $7
  fragment  = $9
Mike Samuel
The regex is mistakenly surrounded with ** and **.
Rene Saarsoo
+3  A: 

I know regular expressions are useful but they're not necessary in this situation. The Location object is inherent of all links within the DOM and has a pathname property.

So, to access that property of some random URL you could need to create a new DOM element and then return it's pathname.

An example, which will ALWAYS work perfectly:

function getPath(url) {
    var a = document.createElement('a');
    a.href = url;
    return a.pathname.substr(0,1) === '/' ? a.pathname : '/' + a.pathname;
}

jQuery version: (uses regex to add leading slash if needed)

function getPath(url) {
    return $('<a/>').attr('href',url)[0].pathname.replace(/^[^\/]/,'/');
}
J-P
I know it's an old post, but I really like your method J-P :)
Ben
+1  A: 

The window.location object has pathname, search and hash properties which contain what you require.

for this page

location.pathname = '/questions/441755/regular-expression-to-remove-hostname-and-port-from-url'  
location.search = '' //because there is no query string
location.hash = ''

so you could use

var fullpath = location.pathname+location.search+location.hash
meouw