Flash / Flex / ActionScript/Regular Expressions/RegExp
Содержание
- 1 Case-insensitive, you can add the i flag:
- 2 Creating a Regular Expression Object
- 3 invert a character class by using a caret (^) immediately after the open bracket ([).
- 4 Matching Using a Regular Expression Object
- 5 (*) quantifier matches zero or more times:
- 6 Quantifiers
- 7 RegExp("(A|BC)* *(A);*(B)* *(C)* *(A|B|C)*", "i")
- 8 Regular expression /\w/ is created in ActionScript
- 9 String Methods and Regular Expressions
- 10 To escape a character, simply preface it with a backslash (\). This goes for the backslash character as well.
- 11 To flag it to match globally and multiline, the following will work:
- 12 To know whether at least one match exists.
- 13 To match an optional character or sequence, use the question mark quantifier (?).
- 14 Use ranges with the dash character (-), and you can have multiple ranges in one character class, as well as combine ranges with single characters
- 15 Use the exec() method.
Case-insensitive, you can add the i flag:
package{
import flash.display.Sprite;
import flash.utils.*;
public class Main extends Sprite{
public function Main(){
var reCase:RegExp = new RegExp("abc","i");
trace(reCase.test("aBc"));
}
}
}
Creating a Regular Expression Object
package{
import flash.display.Sprite;
import flash.utils.*;
public class Main extends Sprite{
public function Main(){
var reCase:RegExp = new RegExp("abc");
trace(reCase.test("aBc"));
reCase.rupile("abc", "i");
trace(reCase.test("aBc"));
}
}
}
invert a character class by using a caret (^) immediately after the open bracket ([).
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
trace("roger dodger".match(/[^oge\s]/g)); //r,r,d,d,r
}
}
}
Matching Using a Regular Expression Object
package{
import flash.display.Sprite;
import flash.utils.*;
public class Main extends Sprite{
public function Main(){
var reCase:RegExp = new RegExp("abc");
trace(reCase.test("aBc")); // Displays: false;
}
}
}
(*) quantifier matches zero or more times:
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
trace("a thousand thousandss!".match(/thousands*/g)); //thousand,thousandss
}
}
}
Quantifiers
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
trace(/\w+:\s*\$\d+/.test("soup: $40")); //true
}
}
}
RegExp("(A|BC)* *(A);*(B)* *(C)* *(A|B|C)*", "i")
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var reCase:RegExp = new RegExp("(A|BC)* *(A);*(B)* *(C)* *(A|B|C)*", "i");
var aMatch:Array = reCase.exec("action");
trace(aMatch);
aMatch = reCase.exec("A B C");
trace(aMatch);
aMatch = reCase.exec("B C");
trace(aMatch);
aMatch = reCase.exec("A B C");
trace(aMatch);
aMatch = reCase.exec("A C B");
trace(aMatch);
}
}
}
Regular expression /\w/ is created in ActionScript
package{
import flash.display.Sprite;
import flash.utils.*;
public class Main extends Sprite{
public function Main(){
var reCase:RegExp = new RegExp("\\w");
trace(reCase.test("aBc"));
}
}
}
String Methods and Regular Expressions
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var reCase:RegExp = new RegExp("(\\w)+", "g");
var sVal = new String("abc def ghi");
var aMatches:Array = sVal.match(reCase);
trace(aMatches.toString());
}
}
}
To escape a character, simply preface it with a backslash (\). This goes for the backslash character as well.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
trace("c:\\windows\\"); //c:\windows\
}
}
}
To flag it to match globally and multiline, the following will work:
package{
import flash.display.Sprite;
import flash.utils.*;
public class Main extends Sprite{
public function Main(){
var reCase:RegExp = new RegExp("abc", "gm");
trace(reCase.test("aBc"));
}
}
}
To know whether at least one match exists.
package{
import flash.display.Sprite;
import flash.utils.*;
public class Main extends Sprite{
public function Main(){
var sEmail:String = new String("someone@someserver.ru");
var reEmail:RegExp = new RegExp("^([\\w\\-\\.]+) ;@(([\\w\\-]{2,}\\.)+[\\w\\-]{2,3})$");
trace(reEmail.test(sEmail));
}
}
}
To match an optional character or sequence, use the question mark quantifier (?).
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var betterPhoneNumber:RegExp = /\(?\d{3}\)?-?\d{3}-?\d{4}/;
trace(betterPhoneNumber.test("(703)222-1234")); //true
trace(betterPhoneNumber.test("310-222-1515")); //true
trace(betterPhoneNumber.test("7242229090")); //true
}
}
}
Use ranges with the dash character (-), and you can have multiple ranges in one character class, as well as combine ranges with single characters
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
trace("abcdefghijklmnopqrstuvwxyz".match(/[a-cmx-z]/g)); //a,b,c,m,x,y,z
}
}
}
Use the exec() method.
package{
import flash.display.Sprite;
import flash.utils.*;
public class Main extends Sprite{
public function Main(){
var reCase:RegExp = new RegExp("abc");
var sVal:String = new String("aBcdefabCdefABC");
var aMatch:Array = reCase.exec(sVal);
aMatch = reCase.exec(sVal);
}
}
}