5 Replies Latest reply on Jan 24, 2014 11:36 AM by lapneto

    Help with search results - Arrays and indexOf

    lapneto Level 1

      Hey guys,

       

      Need help again - I was able to import a txt file using the code below:

      var ofile = util.readFileIntoStream();

      var myfile = util.stringFromStream(ofile);

       

      Now i have to perform a search within this file so, I use am using the following code:

      var pattern = new RegExp("27"+abc,"\g"); //establish the pattern I need, abc is another variable established earlier in the code
      myfile.search(pattern); //search myfile for the pattern above
      myfile.match(pattern); //find matching results of the pattern above
      var count = myfile.match(pattern).length; //create a variable with number of matches found

      if(count>0)

      {
      app.alert("Matches found: "+count) //display in an alert the number of matches
      }

       

      However, now I need to know exactly the position (indexOf) of each of the matches I found in myfile, as I need to extract some characters after the match to input in a specific field of my form.

       

      I guess I need to establish some sort of array and some sort of loop to the next match, therefore could anyone poing me in the right direction?

       

      I am not even sure I am following the right path here, so any help will be much appreciated.

       

      Thanks everyone,

        • 1. Re: Help with search results - Arrays and indexOf
          try67 MVP & Adobe Community Professional

          After you have your matches you can use the "indexOf" method to get their locations, I believe. If there are multiple matches you'll also need to specify the second parameter of this method, to specify from where to start looking...

          • 2. Re: Help with search results - Arrays and indexOf
            lapneto Level 1

            Hi Gilad D, thanks for the usual help.

             

            Could you please provide me with a little more information or more hints?

            I am trying to use the indexOf method, but my matches will indeed be multiple.

            To be honest, I need to get 35 characters following each match and paste it somewhere else, but I am having no luck with the indexOf use...

            Many thanks for your usual help.

            • 3. Re: Help with search results - Arrays and indexOf
              try67 MVP & Adobe Community Professional

              I've written a piece of code that does it with pure text. You can probably adjust it to use the results of your RegExp match:

               

              var text = "Hello, what's your name? my name is Gilad."

              var searchTerm = "name";

              var contextLength = 5;

               

              var matchesCounter = 0;

              var startIndex = 0;

              var matchIndex = text.indexOf(searchTerm,startIndex);

              while (matchIndex!=-1) {

                  matchesCounter++;

                  var context = text.substring(matchIndex-contextLength,matchIndex+searchTerm.length+contextLength);

                  console.println("MATCH " + matchesCounter + " (" + matchIndex + "): " + context);

                  startIndex = matchIndex+searchTerm.length;

                  matchIndex = text.indexOf(searchTerm,startIndex);

              }

               

              The results of running this code are:

              MATCH 1 (19): your name? my

              MATCH 2 (28): ? my name is G

              1 person found this helpful
              • 4. Re: Help with search results - Arrays and indexOf
                lapneto Level 1

                Hi Gilad,

                Thank you so much for your help, adjusting the code above gave me the results I needed!

                Now it will just be a matter of writing the rest of the code (not that it will be easy, but I guess I am on my way!)

                Thanks again!

                • 5. Re: Help with search results - Arrays and indexOf
                  lapneto Level 1

                  Hi Gilad,

                   

                  Suppose i need  to write each of the results to a field (e.g. myField_1, myField_2 etc...).

                   

                  What sort of code would I need to create? I mean, I am no pro and all the code I used was based on the help I get here...

                   

                  Would I need to make it access the field name and increase the end of the field name? (e.g. myField_#+1? Not sure at all)

                   

                  Many thanks again!