Flash / Flex / ActionScript/Regular Expressions/Match
Содержание
- 1 Create a global regex to match multiple times in a string
- 2 Create a global regular expression that matches three-letter words
- 3 Create a pattern to match against a string
- 4 Create a regular expression that matches three-letter words
- 5 Creating a Nongreedy Pattern
- 6 Force the last index to be past the first matching position
- 7 Looking for Pattern Matches
- 8 Return simply whether the string matches the pattern at all.
- 9 Trace the index
Create a global regex to match multiple times in a string
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var example:RegExp = /abc/g;
var result:Array = example.exec( "abc abc" );
trace( result.index );
trace( example.lastIndex );
}
}
}
Create a global regular expression that matches three-letter words
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var regex:RegExp = /\b[a-z]{3}\b/g;
var sentence:String = "This string has two three letter words.";
trace( sentence.search( regex ) );
trace( regex.lastIndex );
trace( sentence.search( regex ) );
}
}
}
Create a pattern to match against a string
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var example:RegExp = /abc/;
trace( example.test( "A string with abc in it" ) );
trace( example.test( "abc" ) );
trace( example.test( "Another string to test against..." ) );
}
}
}
Create a regular expression that matches three-letter words
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var example:RegExp = /\b[a-z]{3}\b/g;
var target:String = "This string has two three letter words";
var result:Array;
while ( ( result = example.exec( target ) ) != null ) {
trace( result );
}
}
}
}
Creating a Nongreedy Pattern
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var example:String = "<b>hello</b>, world!";
trace( example.match( /<.*>/g ) );
}
}
}
Force the last index to be past the first matching position
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var regex:RegExp = /\b[a-z]{3}\b/g;
var sentence:String = "This string has two three letter words.";
trace( sentence.search( regex ) );
trace( regex.lastIndex );
trace( sentence.search( regex ) );
regex.lastIndex = 13;
trace( sentence.search( regex ) );
trace( regex.lastIndex );
}
}
}
Looking for Pattern Matches
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var example:String = "ActionScript 3 Cookbook";
trace( example.search( /script/i ) );
trace( example.search( /script/ ) );
}
}
}
Return simply whether the string matches the pattern at all.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var phoneNumberPattern:RegExp = /\d\d\d-\d\d\d-\d\d\d\d/;
trace(phoneNumberPattern.test("347-222-2225")); //true
trace(phoneNumberPattern.test("Call 800-123-4567 now!")); //true
trace(phoneNumberPattern.test("Call now!")); //false
}
}
}
Trace the index
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var example:RegExp = /abc/;
var result:Array = example.exec( "A string with abc in it" );
trace( result );
trace( result.index );
result = example.exec( "A string with no match" );
trace( result );
}
}
}