SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: parsing a url

  1. #1
    SitePoint Zealot matches's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    parsing a url

    Hello,

    I have a url that I need to parse. Some of the urls look like below:

    http://www.domain.com/1234-4321_!@crap#@

    I need 1234-4321 from that url. I can very easily get that out. However, sometimes in legacy pages there are some urls that look like this:

    http://www.domain.com/Some-Key-Words/1234-4321_!@crap#@

    The problem I am having is I don't know how to skip past these occasional keywords injected into the url. Since the entire url is a string I can't just say if it's a string then don't take it. Hope that makes sense.

    Thanks for any help.

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the amount of characters following the numbers always the same?
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    SitePoint Zealot matches's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only thing that is always the same is is the domain name

    the key words will be different

    the number after the dash (1234-4321) could be 5 digits

    and everything after the underscore is irrelevant


    Thanks for your help

  4. #4
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here you go:

    Code JavaScript:
    var URL = 'http://www.domain.com/Some-Key-Words/1234-4321_!@crap#@';
    var Numbers = URL.match( /(\d){4}([/-])(\d){0,5}/ );
    Numbers = Numbers[0];
    document.write(Numbers);

    This will 'extract' the first occurance of this pattern "[4 DIGITS][dash][4 or 5 DIGITS]"
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  5. #5
    SitePoint Zealot matches's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks man!


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
  •