Flash / Flex / ActionScript/Regular Expressions/Phone Number

Материал из Web эксперт
Перейти к: навигация, поиск

Extracting phone number

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var contactNumbers:String =
            "Los Angeles: 310-222-2225\n" +
            "New York: 212-222-2499\n" +
            "Boston: 617-222-2222";
        var nyPhone:RegExp = /New York: (\d{3}-\d{3}-\d{4})/;
        var matches:Array = contactNumbers.match(nyPhone);
        trace(matches[1]); //212-222-2499
    }
  }
}



insisting that the line end right after the phone number:

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var officeAndPhone:RegExp = /^([\w\s]+): (\d{3}-\d{3}-\d{4})$/;
        trace(officeAndPhone.test(">>>New York: 212-222-2499")); //false
        trace(officeAndPhone.test("New York: 212-222-2499 (fax)")); //false

    }
  }
}



Match a phone number

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var document:String = "My name is John Doe, SSN 123-45-6789,I live at 120 Birch st...";
        var ssn:RegExp = /\d{3}-\d{2}-\d{4}/g;
        document = document.replace(ssn, "<SSN REMOVED>");
        trace(document);
    }
  }
}