0

I have a long url route and I only need to increment a section of the route. Right code to increment this is very crude looking and seems buggy. I'm currently using regex in javascript, but maybe that too complicated:

/superbowl-XIX-group01-01/offense/11

I want to increment the group # route to this using javascript:

/superbowl-XIX-group02-01/offense/11

And I'm currently using this code:

        var nextRosterStr = "";
        nextRosterStr = $('#btnShowMore').attr("next-page");
        if (typeof nextRosterStr === 'undefined') {
            return;
        } else if (isUIButton === false) {
            // update the btnShowMore attribute as well
            var gNumberValue = nextRosterStr.match(/-group0?\d*-/g);
            gNumberValue = gNumberValue.toString().replace(/0/, "ZERO");
            var gNumberStr = gNumberValue.toString().match(/\d*/g);
            var gNumber = parseInt(gNumberStr.join(""), 10);
            gNumber++;
            var gNumberValueFinal = "";
            if (gNumber > 9) {
                gNumberValueFinal = gNumberValue.replace(/\d*/g, gNumber.toString(10));
                gNumberValueFinal = gNumberValueFinal.toString().replace("ZERO", "").toString();
            } else {
                gNumberValueFinal = gNumberValue.toString().replace(/\d+/g, gNumber.toString(10));
                gNumberValueFinal = gNumberValueFinal.toString().replace("ZERO", "0").toString();
            }

            var nextRosterStr = nextRosterStr.replace(/-group0?\d*-/g, gNumberValueFinal.toString());

btw here is a route setup example:

                new RouteMappingItem()
                    {
                        PageName   = "Superbowls",
                        Url        = "/{GameType}-{GameIdentifier}-group{groupNumber}-01/{formationName}/{rankingCount}",
                        Defaults   = new { controller = ...
                    },

There has got to be a simpler way to do this.

Marcus Noon
  • 2,265
  • 2
  • 12
  • 11
  • Maybe you can do it quickly with notepad++. Check this post http://stackoverflow.com/questions/25428620/regular-expression-notepad-increment-numbers-in-every-line/25429806#25429806 – Federico Piazza Aug 28 '14 at 19:36
  • If you are defining the route, why not just specify each section as its own part of the route. IOW `/superbowl-XIX/2/1/offense/11`, where your route definition defines which part is what. – Heretic Monkey Aug 28 '14 at 19:36
  • Needs to be done in javascript b/c I am constantly getting new URLs but the -group#- always stays the same. This is dynamic changes not a one time thing. – Marcus Noon Aug 28 '14 at 19:39
  • Mike: I added a routing example. But can't do this in razor... needs to be done in JavaScript. – Marcus Noon Aug 28 '14 at 19:52
  • I was thinking it would be easier to parse in JS if the URL only had a number in a particular place, not a bunch of extraneous information. If it was closer to what I showed, you could easily parse the URL just by parsing the text between the second and third slashes. Just a thought, which is why it's a comment :). – Heretic Monkey Aug 28 '14 at 19:54
  • True dat dude. I feel ya. – Marcus Noon Aug 29 '14 at 01:53

1 Answers1

1

Don't have different cases for less than 10 and greater or equal 10. Get the number, parse it, increment it, convert it to a string with leading zero, and put it back in the string.

Here is a trick to convert to string with leading zero:

numberAsString = ('00' + number).slice(-2);

Example

Volune
  • 4,324
  • 22
  • 23