Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2002
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    reg expression - remove char

    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

  • #2
    JUD
    JUD is offline
    New Coder
    Join Date
    Jul 2005
    Location
    I'm right here
    Posts
    92
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Just use location.pathname

    Code:
    var path = location.pathname;
    document.write(path);
    You could also do it the way you wanted to do it like this

    Code:
    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.
    Last edited by JUD; 09-07-2006 at 01:15 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •