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 to the CF scene
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Search, Replace and Back Reference

    I'm trying to modify a page's url tags. For some reason, I can't get it to reference digits.

    Here's an original url: http://www.example.com/browse.php?page=1234
    I'm tring to modify to: http://www.example.com/browse/page/1234/fix

    This is what I'm using.
    Code:
    var links = document.getElementsByTagName('a');
    for (i = 0; i < links.length; i++) {
    	links[i].href = links[i].href.replace('\/browse\.php\?page=', '\/browse\/page\/');
    }
    Above works but I can't append '/fix' to the end of that line. Normally, I would backreference
    Code:
    \/browse\.php\?page=(\d+)', '\/browse\/page\/\1\/fix'
    But I can't capture those digits. Am I'm not escaping something? Should be simple.

    Even if I just use '\/browse\.php\?page=\d+', it won't match the above url?

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Code:
    var links = document.getElementsByTagName('a');
    for (i = 0; i < links.length; i++) {
    		 var s = links[i].search;
    		 var l = links[i].href;
    		 links[i].href = l.replace(/\.php/,'/page/' + s.replace(/\?page=(\d+)/,'$1'));
    		 links[i].search = '';
    }
    regards

  • Users who have thanked oesxyl for this post:

    Tobai (10-03-2008)


  •  

    Posting Permissions

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