9 Replies Latest reply on Feb 15, 2017 2:41 AM by foltman

    question mark in javascript/grep expression

    foltman Level 1

      Hello!

       

      I've got problem with question mark character in my code. When I'm trying to use it in expression such as '[!?] \l' (find any '?' or '!' fallowed by lowercase letter) it gives my only those strings with '!', but not with '?'.

      The same time there is no problem with other kinds of expressions with '?' character. It seems that placing it into a square parentheses causes the problem.

       

      I've tried to use escape characters - '\', double '\\', even '/'; but without result.

       

      Where is the problem?

       

      Thanks in advance

      jf

        • 1. Re: question mark in javascript/grep expression
          Loic.Aigon Adobe Community Professional

          This should work:

          [!\?]\l

           

          if as a string, double the backslash

          "[!\\?]\\l"

          • 2. Re: question mark in javascript/grep expression
            foltman Level 1

            Here is my code:

             

            function fnFindChange(findProperties, changeProperties, applySearchTo) {
                app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
                includeFootnotes = true;
                includeHiddenLayers = false;
                includeLockedLayersForFind = false;
                includeLockedStoriesForFind = false;
                includeMasterPages = false;
                app.findGrepPreferences.properties = findProperties;
                app.changeGrepPreferences.properties = changeProperties;
                applySearchTo.changeGrep();
                app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
            };
            
            function fnFindGrep(findProperties, applySearchTo) {
                app.findGrepPreferences = NothingEnum.nothing;
                includeFootnotes = true;
                includeHiddenLayers = false;
                includeLockedLayersForFind = false;
                includeLockedStoriesForFind = false;
                includeMasterPages = false;
                app.findGrepPreferences.properties = findProperties;
                var mySearch = applySearchTo.findGrep();
                app.findGrepPreferences = NothingEnum.nothing;
                return mySearch;
            };
            
            function fnUppercase(applySearchTo) {
                // here is problematic expression
                var myTexts = fnFindGrep({findWhat: '[.!\\?] \\l'}, applySearchTo);
                var myUpperTexts = [];
                var myFinds = [];
                for (var i = 0; i < myTexts.length; i++) {
                    myTexts[i] = myTexts[i].contents;
                    myUpperTexts[i] =
                        myTexts[i].slice(0, myTexts[i].length - 1) +
                        myTexts[i].slice(myTexts[i].length - 1, myTexts[i].length).toUpperCase();
                    myFinds.push({
                        strFindWhat: {findWhat: myTexts[i]},
                        strChangeTo: {changeTo: myUpperTexts[i]}
                    });
                
                }
                for (var j = 0; j < myTexts.length; j++) {
                    fnFindChange(myFinds[j].strFindWhat, myFinds[j].strChangeTo, applySearchTo);
                } 
            }
            fnUppercase(app.activeDocument);
            
            
            
            
            

             

            When I'm using '[!\\?]\\l' not as argument of my function fnFindGrep, just as string of findWhat grep properietie, it works. But in my code it fails.

            I'm trying to achive script that will take every lowercase character that fallows '?', '!', '.' and change it to uppercase.

             

            jf

            • 3. Re: question mark in javascript/grep expression
              Loic.Aigon Adobe Community Professional

              Have you noticed that you have an extra space in your code:

               

              '[.!\\?] \\l'

               

              You look for either a dot, a question or exclamation mark followed by a space then a lowercase character.

               

              if this space is possibly here but not in all cases, use

               

              '[.!\\?] ?\\l'

               

              You may also try

               

              '[!\\?] ?|\\K.'

               

              '(?<=[!\\?] ).'

              • 4. Re: question mark in javascript/grep expression
                Vamitul Level 4

                Guesswork ahead!!!

                If you run the code from the script panel it should work.

                The issue you are facing is that ESTK passes the script to InDesign as a CDATA block in an xml, and it misshandles the '[!'

                Alternatively, you could split the expression as:

                var myTexts = fnFindGrep({findWhat: '['+'!?] \\l'}, applySearchTo); 

                1 person found this helpful
                • 5. Re: question mark in javascript/grep expression
                  foltman Level 1

                  Hi guys!

                  Sorry for the delay, I was traveling a bit.

                   

                  I've tried all of your methods, non is working for me.

                   

                  Zrzut ekranu 2017-02-15 o 09.21.44.png

                  Above is my exemplar text.

                   

                  Loic.Aigon, thanks for spotting extra space in my code (in '[.!\\?] \\l') - but as you can see, it is no accident.

                   

                  Vamitul, good to know that 'ESTK passes the script to InDesign as a CDATA block in an xml', I will bare this in mind next time, but I'm running discussed script straight from Id panel. Your approach with:

                   

                  var myTexts = fnFindGrep({findWhat: '['+'!?.] \\l'}, applySearchTo);
                  

                   

                  works only for dot and exclamation mark, quotation mark is still missed.

                   

                  Any other suggestions where I'm making mistake? Thanks!

                  • 6. Re: question mark in javascript/grep expression
                    Vamitul Level 4

                    Stepping through your code it's fairly easy to understand the mistake.

                    Let's use the following input text as example:

                     

                    ala bala? portocala

                    ala bala. portocala

                    ala bala! portocala

                     

                    Your initial grep search ('[.!?] \\l') works just fine, and it returns an 3 element array:
                    ? p. p

                    ! p

                     

                    Then those three finds are used to create a new grep search, and I hope you can now already see the issue:

                    instead of searching for '\\? p' you are searching for just '? p' where the '?' is considered a regex special character (even more, the '. p' will replace any character followed by a space and a 'p' - nasty).

                     

                    So, what to do:
                    Instead of using a changeGrep to convert the stuff to upercase simply iterate through the found results and fix the stuff:

                     

                    function fnUppercase(applySearchTo) {  
                        var myTexts = fnFindGrep({findWhat: '[.!?] \\l'}, applySearchTo),
                        lastLetter;  
                        for (var i = 0; i < myTexts.length; i++) {  
                            lastLetter = myTexts[i].characters[-1];  
                            lastLetter.contents=lastLetter.contents.toUpperCase();
                          }
                      }
                    

                     

                    Easy-peasy!

                    • 7. Re: question mark in javascript/grep expression
                      Masood.Ahmad Level 1

                      Try any of these:

                       

                      [?!]\l

                       

                      [!\x{003F}]\l

                      Since '?' question mark is a reserved character, the Find/Change might not be accepting it. Instead use its unicode value in your code.

                       

                      Hope that resolves your problem.

                      1 person found this helpful
                      • 8. Re: question mark in javascript/grep expression
                        foltman Level 1

                        Thank you, Masood.Ahmad, it is very helpful remark.

                        In the end I like Vamitul approach better than mine.

                        • 9. Re: question mark in javascript/grep expression
                          foltman Level 1

                          Easy-peasy! - for you. Thanks a lot, it is exactly what I was looking for: much cleaner and straight-forward solution.

                           

                          Because I'm not a professional, not even not a programmer, sometimes I tend to complicate things too much. Good to have all of you, guys!