Skip to main content
Inspiring
October 9, 2017
Answered

How to find % and )% in sigle command

  • October 9, 2017
  • 2 replies
  • 629 views

Hi,

I used the below code to find the percentages (%) and parenthesis  ) ,  in a table, and fix the column width into 10 pts.

But i have an occurrence  for both ) and % (parenthesis with %) .  How to find these occurrences in a single command. Please suggest anyone.

else if(cont.indexOf("%") !=-1)

                    {

                            col.width = "10pt";

                            allcolwidth += col.width;

                        }

                   else if(cont.indexOf(")") !=-1)

                    {

                            col.width = "10pt";

                            allcolwidth += col.width;

Regards,

Velu

This topic has been closed for replies.
Correct answer pixxxelschubser

Hi VeluVK​,

one way could be:

// required a table with one cell as minimum

var aDoc = app.activeDocument;

alert("contents of cell[0]: "+aDoc.stories[0].tables[0].cells[0].contents);

var celCon = aDoc.stories[0].tables[0].cells[0].contents;

var anArrayOfPercentOrParenthesisMatch = celCon.match (/\)|\%/g);

if (anArrayOfPercentOrParenthesisMatch != null) {

    alert(anArrayOfPercentOrParenthesisMatch);

    } else {

        alert("no percent sign or closing parenthesis found in cells[0]");

        }

Have fun

2 replies

VeluVKAuthor
Inspiring
October 11, 2017

Hi pixxxel schubser,

Thanks so much for the help.

Regards,

Velu

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
October 9, 2017

Hi VeluVK​,

one way could be:

// required a table with one cell as minimum

var aDoc = app.activeDocument;

alert("contents of cell[0]: "+aDoc.stories[0].tables[0].cells[0].contents);

var celCon = aDoc.stories[0].tables[0].cells[0].contents;

var anArrayOfPercentOrParenthesisMatch = celCon.match (/\)|\%/g);

if (anArrayOfPercentOrParenthesisMatch != null) {

    alert(anArrayOfPercentOrParenthesisMatch);

    } else {

        alert("no percent sign or closing parenthesis found in cells[0]");

        }

Have fun

Jongware
Community Expert
Community Expert
October 9, 2017

pixxxel,,

1. You don't need to escape the '%' (although it doesn't do something else either);

2. 'This-or-that' single character is more efficiently tested with [%)] -- if only because parentheses lose their magical properties inside a custom character class.

pixxxelschubser
Community Expert
Community Expert
October 9, 2017

Theunis you are totally right. A character class is the better way.

(And I always escape the percent sign - this combi never can muddle up with the modulo operator)