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 3 of 3

Thread: RegExp URI Hash

  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts

    RegExp URI Hash

    Hey there, I'm looking to have some functionality similar to Twitter in that pages are loaded through ajax depending on the path that is shown in the URI hash.

    For example: http://twitter.com/#!/BarackObama/following

    I could just split the string by "/" but I want it to work if you enter a bunch of forward slashes as well, or none at all (only for the first forward slash), here's an example:

    http://twitter.com/#///////////BarackObama/following
    http://twitter.com/#BarackObama/following
    http://twitter.com/#/BarackObama

    The hash would be broken down into multiple parts, the first parameter (ie. BarackObama) would be the first returned value from the match, the second would be the rest of the parameters.

    Here are examples of what the hash would be, and what I would like in return.

    Hash:
    1. http://twitter.com/#/BarackObama/following/test
    2. http://twitter.com/#///////////BarackObama/following
    3. http://twitter.com/#BarackObama

    Returned value from RegExp match:
    1. BarackObama,following/test
    2. BarackObama,following
    3. BarackObama

    Hope that makes it pretty clear. I also have this so far, but it returns both a comma separated list, but also the original string.

    Code:
    /([0-9A-z]+)\/?(.*)/

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Try this:
    Code:
    var myURL = 'put URL in here';
    var myMatch = myURL.match(/#\/*([A-z0-9]+)\/*(.*)$/);
    if(myMatch) {
       for(i=1; i<myMatch.length; i++) {
          alert(myMatch[i]);
       }
    }

  • Users who have thanked devnull69 for this post:

    iLochie (07-18-2011)

  • #3
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    ERM, edit: I'm still having the same problem as before, the first returned value is the entire string. Edit again: Nevermind, I see you have to start at key #1 to return the first value, as opposed to #0.


  •  

    Posting Permissions

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