Skip to main content
Techi_Panda
Inspiring
September 29, 2011
Answered

Matching brackets

  • September 29, 2011
  • 2 replies
  • 1021 views

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

This topic has been closed for replies.
Correct answer Jongware

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.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);

2 replies

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
October 3, 2011

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.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);

Jongware
Community Expert
Community Expert
September 29, 2011

Dead simple.

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

Techi_Panda
Inspiring
September 29, 2011

Hi Jongware

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

Jongware
Community Expert
Community Expert
September 29, 2011

For CS3? No.

It's really simple, though. Please try.