4 Replies Latest reply on Jul 25, 2014 7:49 AM by Teetan VK

    How Can find numbers and make it plus 1

    Teetan VK Level 1

      Hi, everyone

       

      I want find a bunch of numbers in my slection and then make it + 1

       

      for instance:

       

      aged (\d\d)

      I want to make it + 1

       

      and

       

      201(\d)

       

      I want to make it + 1

       

      what's the syntax?

       

      thanks

       

      VK

        • 1. Re: How Can find numbers and make it plus 1
          Chinnadk Level 4

          Hi VK,

           

          Try this. It finds the number in the selection and adds one to it, for example it finds "aged (20)", "201(2)" and will replace it to "aged (21)", 201(3) respectively .

           

          var _selection = app.selection[0];
          app.findGrepPreferences = changeGrepPreferences = null;
          app.findGrepPreferences.findWhat = "(aged) (\\(\\d\\d\\))";
          var found = app.selection[0].findGrep();
          for(var i=0;i<found.length;i++)
          {
                  app.findGrepPreferences = changeGrepPreferences = null;
                  app.findGrepPreferences.findWhat = "\\d\\d";
                  var foundnum = found[i].findGrep();
                  var newnum = parseInt(foundnum[0].contents) + 1;
                  app.changeGrepPreferences.changeTo = newnum.toString();
                  found[i].changeGrep();
              }
          app.findGrepPreferences = changeGrepPreferences = null;
          
          
          app.findGrepPreferences.findWhat = "201\\(\\d\\)";
          var found1 = app.selection[0].findGrep();
          for(var i=0;i<found1.length;i++)
          {
                  app.findGrepPreferences = changeGrepPreferences = null;
                  app.findGrepPreferences.findWhat = "\\d\\)";
                  var foundnum = found1[i].findGrep();
                  var newnum = parseInt(foundnum[0].contents) + 1;
                  app.changeGrepPreferences.changeTo = newnum.toString() + ")";
                  found1[i].changeGrep();
              }
          app.findGrepPreferences = changeGrepPreferences = null;
          

           

          Regards,

          Chinna

          • 2. Re: How Can find numbers and make it plus 1
            Teetan VK Level 1

            Hi, Chinna

             

            I really appreciate for your help!

             

            thank you so much

             

            Regards,

             

            VK

            • 3. Re: How Can find numbers and make it plus 1
              Peter Kahrel Adobe Community Professional & MVP

              Chinna,

               

              You need to process the found array back to front, like this:

               

              for (var i = found1.length-1; i >= 0; i--)


              because if you don't, you get problems when the found numbers change length ( 9 > 10, 99 > 100).


              Peter

              • 4. Re: How Can find numbers and make it plus 1
                Teetan VK Level 1

                thanks Peter

                 

                but I got no any problem,

                 

                it runs perfectly

                 

                VK