开发者

regex strip domain name

开发者 https://www.devze.com 2023-02-14 12:39 出处:网络
A quick simple regex question I have a domain name in a string that I need to strip - There is always http://ww开发者_JAVA技巧w. and the domain always ends in \"/\"

A quick simple regex question

I have a domain name in a string that I need to strip - There is always http://ww开发者_JAVA技巧w. and the domain always ends in "/"

g_adv_fullpath_old = g_adv_fullpath_old.replace(/http\:\/\/www\.(.*?)\//ig, '');

how do I create the regex to strip the domain name?

Any help would be appreciated


I would simply split on "/". For example:

>>> "http://www.asdf.com/a/b/c".split("/").slice(3).join("/")
'a/b/c'


Why complications? Simple indexOf will do.
First remove http://www (10 characters), then everything before the first slash.

var s = "http://www.google.com/test";
s = s.substr(10);
s = s.substr(s.indexOf('/'));
alert(s);

Or split, as David suggests.

An example


If you are looking to remove the http://www. and the following slash (plus anything after it) Try:

g_adv_fullpath_old.replace(/http:\/\/www\.(.*?)\/.*/ig, '$1')


You can also extend the stringobject so it supports urlParts

Example

http://jsfiddle.net/stofke/Uwdha/

Javascript

String.prototype.urlParts = function() {
    var loc = this;
    loc = loc.split(/([a-z0-9_\-]{1,5}:\/\/)?(([a-z0-9_\-]{1,}):([a-z0-9_\-]{1,})\@)?((www\.)|([a-z0-9_\-]{1,}\.)+)?([a-z0-9_\-]{3,})((\.[a-z]{2,4})(:(\d{1,5}))?)(\/([a-z0-9_\-]{1,}\/)+)?([a-z0-9_\-]{1,})?(\.[a-z]{2,})?(\?)?(((\&)?[a-z0-9_\-]{1,}(\=[a-z0-9_\-]{1,})?)+)?/g);
    loc.href = this;
    loc.protocol = loc[1];
    loc.user = loc[3];
    loc.password = loc[4];
    loc.subdomain = loc[5];
    loc.domain = loc[8];
    loc.domainextension = loc[10];
    loc.port = loc[12];
    loc.path = loc[13];
    loc.file = loc[15];
    loc.filetype = loc[16];
    loc.query = loc[18];
    loc.anchor = loc[22];
    //return the final object
    return loc;
};

Usage:

 var link = "http://myusername:mypassword@test.asdf.com/a/b/c/index.php?test1=5&test2=789#tite";
 var path = link.urlParts().path;
 var path = link.urlParts().user;
0

精彩评论

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