2 Replies Latest reply on Oct 5, 2011 5:13 PM by Francisc

    RegExp match position

    Francisc Level 3



      Is it possible to loop through the results of a match operation and know where in the string each value was found?


      Thank you.

        • 1. Re: RegExp match position

          The example in the ASDoc for Regexp.exec() contains exactly what you're looking for:




          The key thing is to pass "g" as a flag into the RegExp so that it runs subsequent searches using the lastIndex from the previous match, and then call exec() repeatedly until no more matches exist.


          Here's a standalone example of this in action:



          <?xml version="1.0" encoding="utf-8"?>

          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"


                         xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">







                      protected function searchButton_clickHandler(event:MouseEvent):void


                          var resultText:String = "";


                          var pattern:RegExp = new RegExp(searchString.text, "ig");

                          var text:String = searchText.text;


                          var searchResults:Array = pattern.exec(text);


                          while (searchResults != null)


                              resultText += "Position " + searchResults.index + ":\t" + searchResults[0] + "\n";


                              // run search again

                              searchResults = pattern.exec(text);



                          results.text = resultText;






                  <!-- Place non-visual elements (e.g., services, value objects) here -->


              <s:HGroup verticalAlign="middle">

                  <s:Label text="Search text:" fontWeight="bold" />

                  <s:TextArea id="searchText" width="400">

                      <s:text>Senate Democratic leaders on Wednesday proposed a 5 percent surtax on people with incomes of more than $1 million a year to pay for the package of job-creation measures sought by President Obama and to quell a brewing revolt among Democrats against the White House plan


                          The Senate majority leader, Harry Reid, left, Democrat of Nevada, speaks as Senator Charles E. Schumer of New York listens during a news conference on Wednesday.



                          The Senate majority leader, Harry Reid, Democrat of Nevada, said the surtax would raise $445 billion over 10 years, just about the amount needed to pay for the jobs bill. Mr. Reid said his proposal would “have the richest of the rich pay a little bit more” — “5 percent more to fund job creation and ensure this country’s economic success.”


                          Mr. Reid’s proposal was meant to draw a sharp contrast with Republicans and to win over Democrats who were resisting the president’s proposal due to the tax increases he had suggested.


                          The approach is unlikely to win any backing from Republican leaders who strenuously oppose increases in tax rates, saying they would put a damper on the economy and penalize “job creators.” But the plan, which Senate Democrats had aired last year to a cool response from the White House, is seen by party strategists as having appeal with the public.</s:text>





                  <s:Label text="Search string:" fontWeight="bold" />

                  <s:TextInput id="searchString" text="Senate" />



              <s:Button id="searchButton" label="Search" click="searchButton_clickHandler(event)"/>


                  <s:Label text="Search results:" fontWeight="bold" />

                  <s:TextArea id="results" width="300" height="400"/>








          -- Tom

          • 2. Re: RegExp match position
            Francisc Level 3

            Thank you, Tom.