3 Replies Latest reply on Aug 4, 2016 1:01 AM by Kai Rübsamen

    Strange GREP script behavior?

    beh_gras_beh Level 1

      Dear Indesign scripters,

       

      I am trying to get this GREP to work in Javascript.

      app.findGrepPreferences.findWhat =  "\x{2195} [0-4]\d|[\d]{3,} cm.";
      

       

      It does not seem to work.

      But when I do CMD+F and I take the GREP tab, it seems to work. (CS5).

       

      Btw, I'm trying to find something like this. ↕ 25 cm. And than simply going to remove it.

       

      Do I miss something here?

       

      Thanks for the help.

      Greetings.

        • 1. Re: Strange GREP script behavior?
          [Jongware] Most Valuable Participant

          You are missing that a string that you enter in JavaScript obeys JavaScript rules: its backslash-escaped characters will be translated to its internal representation according to JS's rules before the script engine has a chance to pass it on to InDesign. Therefore, for example, "\d" will be translated to just "d" because "\d" is not a valid code (in JavaScript).

           

          To bypass this, always use double backslashes in strings that are to be passed on. A double backslash is translated to a single, and then the literal string "\d" is correctly put into ID's GREP.

           

          A good way to check this is to look what actually ended up in the Find/Change dialog, by just opening it.

           

          Ah – afterthought. Does this GREP in your post look like the one you actually are using? The pipe | is a tricky one. Without any parentheses, it will divide the entire GREP sequence into 2, a left part and a right part. Then, if the left part is not found, it will try to match the right half. What this means in your case is that it will find "↕ 25" OR "25 cm", not (what you probably intended) "↕ ([01234](any digit) OR (three digits)) cm" – check the parentheses.

           

          You probably need something like

           

          "\\x{2195} ([0-4]\\d|\\d{3,}) cm\\."

           

          Note that, apart from the parentheses around the center part, "[\d]" is a double-up (just "\d" is enough), and you need to escape the dot at the end.

          • 2. Re: Strange GREP script behavior?
            beh_gras_beh Level 1

            Dear Jongware,

             

            I can't imagine an world without Jongware.

            Thank you very much for the help, I will keep this good tip in mind when I use GREP's in JS.

             

            Greetings.

            • 3. Re: Strange GREP script behavior?
              Kai Rübsamen Adobe Community Professional

              What is the idea behind this part?

               

              ([0-4]\d|\d{3,})

               

              Really exclude 50-99, but honor 100 and up, find only integers … makes no sense to me!?

               

              Is there any disadvantage, if I use instead of escaping something like:

               

              /\x{2195} ([0-4]\d|\d{3,}) cm\./.source

               

              Kai