SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    determining a 'tricky' substring

    Hi,

    I'm using location.href to get the url of the current page a user is on and setting that as a variable:

    myURL=location.href;

    I have three levels (depths) of urls that may be retured:

    • hxxp://www.myurl.com/kc/content/ilc/courses/7395/index.html
    • hxxp://www.myurl.com/kc/content/ilc/courses/7395/Tools/index.html
    • hxxp://www.myurl.com/kc/content/ilc/courses/7395/Module/Topic1/index.html


    What I'm try to do with the variable (myURL) is extract just the substring that comes after the '7395/' as a new string so that with the above examples, my new string would be 'index.html' or 'Tools/index.html' or 'Module/Topic1/index.html'.

    Can anyone help me out with this?

    Thanks very much in advance!

  2. #2
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    myURL = location.href.substring(49);
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Code javascript:
    var re = /7395\/(.+)/;
    var url = "hxxp://www.myurl.com/kc/content/ilc/courses/7395/Module/Topic1/index.html";
    var path = url.match(re)[1];
    // path is 'Module/Topic1/index.html';
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just out of interest, here's another way:
    Code:
    var path = myUrl.split("/").slice(8).join("/");
    This could be useful if you need to use individual parts of the path.


Bookmarks

Posting Permissions

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