5 Replies Latest reply on May 12, 2016 4:02 PM by Loic.Aigon

    Javascript question

    Mohammad Modassir Level 1

      Hi,

       

      In one of the sample-line in FindChangeList.txt given by Indesign program — I find:

      grep{findWhat:"  +"}{changeTo:" "}

       

      ...where the description is “Find all double spaces and replace with single spaces.”

       

      Question 1:

      Why is there “+” after double-space in “findWhat:” and not after single space in “changeTo:”?

       

       

       

      Question 2:

      If I want for example to find letter “ B ” or said in javascripting-language “<space>B<space> ” — standing alone with one space on each side — do I have to use a +-sign i.e “<space>B<space>+” or just “<space>B<space> ”  in the “findWhat”?

       

      Would be glad for your help.

       

      Regards Modassir

        • 1. Re: Javascript question
          Loic.Aigon Adobe Community Professional

          Modassir,

           

          grep{findWhat:"  +"}{changeTo:" "}

           

          as indicated is a grep find/change operation. So the findWhat indicates any combination of text that matches the pattern. ChangeTo is what is going to replace the match as a "static" string.

           

          The + is a metacharacters that says:"please indesign find either one space characters, continous two spaces characters and so on to as many continous spaces you can find". Once you found all of them change those to one space only…

           

          It's likely this will make more sense to you once you start learning regular expressions…

           

          Loic

          • 2. Re: Javascript question
            Mohammad Modassir Level 1

            Hi, Loic, thanks for you reply , but...

            Don’t you mean ..."please indesign find either two space characters, continous three spaces characters and so on...” i.e ignore one-space?

            • 3. Re: Javascript question
              Loic.Aigon Adobe Community Professional

              no, + means one to many spaces. See here :

              Regular expression - Wikipedia, the free encyclopedia

              • 4. Re: Javascript question
                [Jongware] Most Valuable Participant

                You are correct. The "+" character repeats:

                 

                1. the regular character immediately before it (when not a special character, or the "match anything" Full Stop)
                2. the character class before it: [a-z]+, for example, will match any sequence of lowercase characters;
                3. the entire group before it: (abc)+, for example, will match any sequence of "abcabcabc.." in that order.

                 

                So your "space space +" will first match a single space, then followed by any positive number of next spaces. In regular English, that is two or more spaces.

                 

                It would still work the same with a single "space +" but then it would also match a single space - and replace it again with a single space, i.e., nothing visibly changes. The difference is that there may be thousands of single spaces in your document, and InDesign will happily find-and-change all of them to a single space. So the entire operation would just take up more time, and without any practical use.

                 

                I've found that finding is fast, but changing as little possible can speed up find-and-change operations drastically. Suppose you want to change all regular spaces in "Figure 1" (and all other numbers) to fixed spaces. You could do this:

                 

                Find: Figure (\d)

                Change: Figure~S$1

                 

                but this ought to be faster:

                 

                Find: (?<=Figure) (?=\d)

                Change: ~S

                 

                ... changing only one single character, the space itself.

                • 5. Re: Javascript question
                  Loic.Aigon Adobe Community Professional

                  oh my bad I didn't notice there were two spaces in the findWhat property. I saw only one hence my answer.