4 Replies Latest reply on Oct 3, 2011 2:04 PM by [Jongware]

    Matching brackets

    Techi Panda Level 2

      Hi all

       

      I am in the process of matching opening and closes braces, brackets, squre brackes by using ID CS3 Javascript. Kindly suggest me how to start.

       

      Simple method:

       

      Find open brace and close brace count for found items and compare. This will give global result but I need to trace which one of the opening or closing brace doesnot have pair.

       

      Thanks in advance!

       

      Regards

      arul

        • 1. Re: Matching brackets
          [Jongware] Most Valuable Participant

          Dead simple.

           

          Find any open OR closed bracket in the correct order and maintain a counter.

          • 2. Re: Matching brackets
            Techi Panda Level 2

            Hi Jongware

             

            Thanks a lot for your suggestion. Can you please give me some code example of your method so that I can pickup

            • 3. Re: Matching brackets
              [Jongware] Most Valuable Participant

              For CS3? No.

               

              It's really simple, though. Please try.

              1 person found this helpful
              • 4. Re: Matching brackets
                [Jongware] Most Valuable Participant

                Here is some fun to git you started with. (I was real bored.)

                 

                app.findGrepPreferences = null;
                app.findGrepPreferences.findWhat = "[()[\\]<>{}]";
                
                someBrackets = app.activeDocument.findGrep();
                allBrackets = '';
                for (i=0; i<someBrackets.length; i++)
                          allBrackets += someBrackets[i].contents;
                
                do
                {
                          i = allBrackets.indexOf("()");
                          if (i < 0)
                                    i = allBrackets.indexOf("<>");
                          if (i < 0)
                                    i = allBrackets.indexOf("[]");
                          if (i < 0)
                                    i = allBrackets.indexOf("{}");
                          if (i >= 0)
                                    allBrackets = allBrackets.substr(0,i)+allBrackets.substr(i+2);
                } while (i >= 0);
                alert (allBrackets);