开发者

Regular Expression for Google Analytics to determine page

I'm looking speci开发者_如何学Pythonfically for a regular expression that will grab the last term of a URL. This is not always a file name, it may not end in .html or .php, so I'll need to make sure that the regular expression is grabbing the last term from the URL.


Example:

I need to grab www.mydomain.com/anything_can_be_here/thankyoupage

I need to extract "thankyoupage" even when there can be any term preceding it in the URL.

Also note, there is no file extension on the thankyoupage URL segment.


This should do it:

/^(?:http:\/\/)?(?:[^\/]+)\/.*?\/([^\/]+)(?:\?.*)?$/

For example, the result of this:

m = 'http://example.com/where/is?the=pancakes/house'.match(/^(?:http:\/\/)?(?:[^\/]+)\/.*?\/([^\/]+)(?:\?.*)?$/);

is this array:

["http://example.com/where/is?the=pancakes/house", "is"]

And this:

m = 'http://example.com/where/is'.match(/^(?:http:\/\/)?(?:[^\/]+)\/.*?\/([^\/]+)(?:\?.*)?$/)

Results in:

["http://example.com/where/is", "is"]

And this:

m = 'http://example.com/'.match(/^(?:http:\/\/)?(?:[^\/]+)\/.*?\/([^\/]+)(?:\?.*)?$/)

Results in null.

And your component is in m[1] and that comes from ([^\/]+). The (?:[^\/]+) will take care of the hostname (and the userinfo if it happens to be present), the (?:\?.*)?$ part will take care of any trailing CGI arguments.

Depending on your URLs, you could replace ^(?:http:\/\/)? with ^http:\/\/.


If you are only feeding it urls, something simple as .*/(.*) should work

that's assuming there is a '/' after the .com/.org/whatever

otherwise you'll get everything after the http://


what you need is the path name, which can be access using:

window.location.pathname;


Try this regex:

^http:\/\/.*/(.+)$

It will look for string starting with http:// then will go all the way till the last / and store everything after the last / into $1 variable.


The regexp:

/(\/([^\/]+))+/g

Take the 3rd element of the resulting array:

var a='http://www.host.com/aaa/bbb/ccc/dd.pp';
var regexp=/(\/([^\/]+))+/g;
var result=regexp.exec(a)
if( result.length==3) {
    document.write('<p>'+result[2]+'</p>');
} else {
    document.write('<p>Fail</p>');
}


Try this:

var str = "www.mydomain.com/other/other/this";  
var path = /(?:https?:\/\/)?(?:www\.)?.*\/([^\/]+)/.exec(str)[1]; //this


Hope this is what you want

console.log(window.location.pathname.split('/').reverse()[0]);


Alright figured it outmyself, thanks anyways guys

/\/*\/thanks/

will match /thanks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜