Chapter 12 Example 4

by actionscriptbible
♥0 | Line 30 | Modified 2009-07-23 05:43:40 | MIT License
play

ActionScript3 source code

/**
 * Copyright actionscriptbible ( http://wonderfl.net/user/actionscriptbible )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/36fl
 */

package {
  import com.actionscriptbible.Example;
  
  public class ch12ex4 extends Example {
    public function ch12ex4() {
      //EXTRACTING
      
      trace("---- match()");
      var contactNumbers:String = 
        "Call us at one of these numbers.\n" +
        "Los Angeles: 310-555-2910\n" + 
        "New York: 212-555-2499\n" + 
        "Boston: 617-555-7141";
      var nyPhone:RegExp = /New York: (\d{3}-\d{3}-\d{4})/;
      var matches:Array = contactNumbers.match(nyPhone);
      trace(matches[1]); //212-555-2499
      
      trace("---- exec()");
      var contacts:Object = new Object();
      
      var contactText:String = 
          "Call us at one of these numbers.\n" +
          "Los Angeles: 310-555-2910\n" + 
          "New York: 212-555-2499\n" + 
          "Boston: 617-555-7141";
      var officeAndPhone:RegExp = /^([\w\s]+): (\d{3}-\d{3}-\d{4})/gm;
      
      var result:Object;
      while (result = officeAndPhone.exec(contactText)) {
          contacts[result[1]] = result[2];
      }
      
      trace(contacts["New York"]); //212-555-2499
      trace(contacts["Boston"]); //617-555-7141
    }
  }
}