Question

I'm looking for the equivalent of "document.referrer.pathname". I know there are other questions that are similar to this on SO, but none of them handle all the use cases. For example:

http://example.com/RESULT

http://example.com/RESULT/

http://example.com/RESULT?query=string

All examples should return:

RESULT

or

https://example.com/EXTENDED/RESULT/

EXTENDED/RESULT

Some folks may want the trailing slash included, but I don't because I'm matching against a list of referrers.

I've started with:

document.referrer.match(/:\/\/.*\/(.*)/)[1]

and am struggling adding the query string parsing.

Thanks!

Was it helpful?

Solution 2

Try this regular expression:

.match(/\/\/.*?\/(.*?)\/?(\?.*)?$/)[1]

DEMO

OTHER TIPS

If you have URLs as strings you can create empty anchors and give them the url as href to access the pathname:

var url = 'http://example.com/RESULT?query=string', // or document.referrer
    a = document.createElement('a');
a.href = url;
var result = a.pathname.replace(/(^\/|\/$)/g,'');

I set up a test example for you here: http://jsfiddle.net/eWydy/

If you don't want to create a new element for it or rely on a.pathname, I'd suggest using indexOf and slice.

function getPath(s) {
    var i = s.indexOf('://') + 3, j;
    i = s.indexOf('/',i) + 1;  // find first / (ie. after .com) and start at the next char
    if( i === 0 ) return '';
    j = s.indexOf('?',i); // find first ? after first / (as before doesn't matter anyway)
    if( j == -1 ) j = s.length; // if no ?, use until end of string
    while( s[j-1] === '/' ) j = j - 1; // get rid of ending /s
    return s.slice(i, j); // return what we've ended up at
}
getPath(document.referrer);

If you want regex though, maybe this

document.referrer.match(/:\/\/[^\/]+[\/]+([^\?]*)[\/]*(?:\?.*)?$/)[1]

which does "find the first ://, keep going until next /, then get everything that isn't a ? until a ? or the last / or end of string and capture it", which is basically the same as the function I did above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top