Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6880445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:58:30+00:00 2026-05-27T04:58:30+00:00

I’ll start right away by saying that I am not a programmer but I

  • 0

I’ll start right away by saying that I am not a programmer but I do like to poke around things and learn. Here’s what I have:

1) URL in the following format: http://site.com/#!/show/me/stuff/1-12/

2) jQuery pagination script that outputs the # of available pages. Each number is a link with href="#pageNumber"

3) jQuery script that binds to all pagination links. The function code is below.

Basically, my goal is to replace 1-12 in my URL with the proper values if, say, I select page #2.

Here’s what I cooked up that works but is extremely ugly:

var pageId = this.href.split("#")[1]; 
// this gets the number from the pagination link (i.e.,
// http://site.com/#2 becomes 2)

var url = location.hash.split("/");
var url = url[url.length-2];
// this is my way of extracting "1-12" from the URL :)

var showFrom = parseFloat(url.split("-")[0]); // 1
var showTo = parseFloat(url.split("-")[1]); // 12
var itemsPerPage= (showTo-showFrom)+1; // 12
var newShowTo = (itemsPerPage*pageId); // 24
var newShowFrom = (itemsPerPage*pageId)-itemsPerPage+1; // 13
var newUrl = newShowFrom+"-"+newShowTo; // 13-24

location.hash = location.hash.replace(url, newUrl);
// http://site.com/#!/show/me/stuff/1-12/ now becomes
// http://site.com/#!/show/me/stuff/13-24/

How can I make this more elegant and in fewer steps?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:58:31+00:00Added an answer on May 27, 2026 at 4:58 am

    Your code is not that ugly – the variable names are self explanatory and I can assure you we have all seen things that are much worse.

    Anyway, here is a way to do the same thing as you did but using regexes:

    var pageId = this.href.split("#")[1];   
    var pattern = /\/(\d+)-(\d+)\/?$/;
    location.hash = location.hash.replace(pattern, function(_, showFrom_str, showTo_str){
        var showFrom = parseInt(showFrom_str, 10);
        var showTo   = parseInt(showTo_str, 10);
        var itemsPerPage = showTo - showFrom + 1;
        return '/' + ((itemsPerPage * pageId) - itemsPerPage + 1) + '-' + (itemsPerPage * pageId) + '/';
    });
    

    How it works:

    The pattern: slash \/, followed by one or more + digits \d, followed by a hyphen -, followed by another sequence of digits (\d+), followed by an optional ? slash \/, followed by the end of the string $.

    The substitution: We can pass a substitution function to the replace method so that we have the replacement text depend of the replaced text. The first argument to the function is the whole match ("/1-12/") and we ignore it (_ is a convention for “ignore this”).

    The next arguments correspond to the captured patterns in the regex – those things wrapped by parenthesis. In your case, the arguments are the string representations of the numbers for the start and the end of the range.

    I then just use the same logic as you did, except that

    • I used parseInt instead of parseFloat. (Don’t forget about the second parameter for the base though or “08” can bite you)
    • I used less intermediate variables.

    In the end all the regex was get rid of the string splitting and streamline the code a bit. However, in a real situation I would probably try to think about a more robust solution that did not depend on fragile string operations on the URL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.