Gerald Ferreira
Gerald Ferreira

Reputation: 1337

regex strip domain name

A quick simple regex question

I have a domain name in a string that I need to strip - There is always http://www. 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

Upvotes: 4

Views: 5184

Answers (4)

Stofke
Stofke

Reputation: 2958

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:[email protected]/a/b/c/index.php?test1=5&test2=789#tite";
 var path = link.urlParts().path;
 var path = link.urlParts().user;

Upvotes: 1

Mike C
Mike C

Reputation: 3117

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')

Upvotes: 2

Nikita Rybak
Nikita Rybak

Reputation: 68006

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

Upvotes: 2

David Wolever
David Wolever

Reputation: 154484

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

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

Upvotes: 9

Related Questions