...

View Full Version : reg expression - remove char



deathmill
09-06-2006, 07:57 PM
hi, am trying to get my head around this and just running into problems.

take this url: http://www.something.com/m/big/cheese.html?mice=1&d=2

i've put together 2 expressions

remove http://www.something.com = smp.replace(/https?:\/\/[^\/]*[\/]*\?(.*)/, "");
remove ?mouse=1&dead=2 = smp.replace(/\?(.*)/, "");

how do i join them together so I can remove both and only have /m/big/cheese.html

thanks in advanced.

D:( :cool:

JUD
09-06-2006, 11:51 PM
Just use location.pathname



var path = location.pathname;
document.write(path);


You could also do it the way you wanted to do it like this



var url = location.href; // The full url -- 'http://www.something.com/m/big/cheese.html?mice=1&d=2'
var protocol = location.protocol; // The protocol -- 'http:'
var host = location.host; // The website address -- 'www.something.com'
var qs = location.search; // The query string -- ?mice=1&d=2

url = url.replace(protocol, ""); // This leaves '//www.something.com/m/big/cheese.html?mice=1&d=2'
url = url.replace(host, ""); // This leaves '///m/big/cheese.html?mice=1&d=2'
url = url.replace(qs, ""); // This leaves '///m/big/cheese.html'
url = url.replace(/^\/{2}/, ""); // This leaves '/m/big/cheese.html'

// Shortcut for the 4 lines above -- url = url.replace(protocol, "").replace(host, "").replace(qs, "").replace(/^\/{2}/ "")

document.write(url);


Both of these will return /m/big/cheese.html.
I know which one I would prefer to use.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum