Package | Top Level |
Class | public dynamic class RegExp |
Inheritance | RegExp Object |
Language version: | ActionScript 3.0 |
Runtime version: |
You can create a new RegExp object by using the new RegExp()
constructor or by
assigning a RegExp literal to a variable:
var pattern1:RegExp = new RegExp("test-\\d", "i"); var pattern2:RegExp = /test-\d/i;
For more information, see "Using Regular Expressions" in the ActionScript 3.0 Developer's Guide.
See also
Property | Defined by | ||
---|---|---|---|
constructor : Object
A reference to the class object or constructor function for a given object instance.
| Object | ||
dotall : Boolean
[read-only]
Specifies whether the dot character (.) in a regular expression pattern matches
new-line characters.
| RegExp | ||
extended : Boolean
[read-only]
Specifies whether to use extended mode for the regular expression.
| RegExp | ||
global : Boolean
[read-only]
Specifies whether to use global matching for the regular expression.
| RegExp | ||
ignoreCase : Boolean
[read-only]
Specifies whether the regular expression ignores case sensitivity.
| RegExp | ||
lastIndex : Number
Specifies the index position in the string at which to start the next search.
| RegExp | ||
multiline : Boolean
[read-only]
Specifies whether the
m (multiline ) flag is set. | RegExp | ||
prototype : Object
[static]
A reference to the prototype object of a class or function object.
| Object | ||
source : String
[read-only]
Specifies the pattern portion of the regular expression.
| RegExp |
Method | Defined by | ||
---|---|---|---|
Lets you construct a regular expression from two strings.
| RegExp | ||
Performs a search for the regular expression on the given string
str . | RegExp | ||
Indicates whether an object has a specified property defined.
| Object | ||
Indicates whether an instance of the Object class is in the prototype chain of the object specified
as the parameter.
| Object | ||
Indicates whether the specified property exists and is enumerable.
| Object | ||
Sets the availability of a dynamic property for loop operations.
| Object | ||
Tests for the match of the regular expression in the given string
str . | RegExp | ||
Returns the string representation of this object, formatted according to locale-specific conventions.
| Object | ||
Returns the string representation of the specified object.
| Object | ||
Returns the primitive value of the specified object.
| Object |
dotall | property |
dotall:Boolean
[read-only]
Language version: | ActionScript 3.0 |
Runtime version: |
Specifies whether the dot character (.) in a regular expression pattern matches
new-line characters. Use the s
flag when constructing
a regular expression to set dotall = true
.
public function get dotall():Boolean
s
(dotall
)
flag on a regular expression:
var str:String = "<p>Hello\n" + "again</p>" + "<p>Hello</p>"; var pattern:RegExp = /<p>.*?<\/p>/; trace(pattern.dotall) // false trace(pattern.exec(str)); // <p>Hello</p> pattern = /<p>.*?<\/p>/s; trace(pattern.dotall) // true trace(pattern.exec(str));
extended | property |
extended:Boolean
[read-only]
Language version: | ActionScript 3.0 |
Runtime version: |
Specifies whether to use extended mode for the regular expression. When a RegExp object is in extended mode, white space characters in the constructor string are ignored. This is done to allow more readable constructors.
Use the x
flag when constructing a regular expression to set
extended = true
.
public function get extended():Boolean
x
flag, causing the white spaces in
the string to be ignored.
var rePhonePattern1:RegExp = /\d{3}-\d{3}-\d{4}|\(\d{3}\)\s?\d{3}-\d{4}/; var str:String = "The phone number is (415)555-1212."; trace(rePhonePattern1.extended) // false trace(rePhonePattern1.exec(str)); // (415)555-1212 var rePhonePattern2:RegExp = / \d{3}-\d{3}-\d{4} | \( \d{3} \) \ ? \d{3}-\d{4} /x; trace(rePhonePattern2.extended) // true trace(rePhonePattern2.exec(str)); // (415)555-1212
global | property |
global:Boolean
[read-only]
Language version: | ActionScript 3.0 |
Runtime version: |
Specifies whether to use global matching for the regular expression. When
global == true
, the lastIndex
property is set after a match is
found. The next time a match is requested, the regular expression engine starts from
the lastIndex
position in the string. Use the g
flag when
constructing a regular expression to set global
to true
.
public function get global():Boolean
g
(global
) flag on the exec()
method:
var pattern:RegExp = /foo\d/; var str:String = "foo1 foo2"; trace(pattern.global); // false trace(pattern.exec(str)); // foo1 trace(pattern.lastIndex); // 0 trace(pattern.exec(str)); // foo1 pattern = /foo\d/g; trace(pattern.global); // true trace(pattern.exec(str)); // foo1 trace(pattern.lastIndex); // 4 trace(pattern.exec(str)); // foo2
ignoreCase | property |
ignoreCase:Boolean
[read-only]
Language version: | ActionScript 3.0 |
Runtime version: |
Specifies whether the regular expression ignores case sensitivity. Use the
i
flag when constructing a regular expression to set
ignoreCase = true
.
public function get ignoreCase():Boolean
i
(ignoreCase
) flag:
var pattern:RegExp = /bob/; var str:String = "Bob bob"; trace(pattern.ignoreCase); // false trace(pattern.exec(str)); // bob pattern = /bob/i; trace(pattern.ignoreCase); // true trace(pattern.exec(str)); // Bob
lastIndex | property |
lastIndex:Number
[read-write]
Language version: | ActionScript 3.0 |
Runtime version: |
Specifies the index position in the string at which to start the next search. This property
affects the exec()
and test()
methods of the RegExp class.
However, the match()
, replace()
, and search()
methods
of the String class ignore the lastIndex
property and start all searches from
the beginning of the string.
When the exec()
or test()
method finds a match and the g
(global
) flag is set to true
for the regular expression, the method
automatically sets the lastIndex
property to the index position of the character
after the last character in the matching substring of the last match. If the
g
(global
) flag is set to false
, the method does not
set the lastIndex
property.
You can set the lastIndex
property to adjust the starting position
in the string for regular expression matching.
public function get lastIndex():Number
public function set lastIndex(value:Number):void
lastIndex
property, and it shows how it is updated after a call to the exec()
method on a
regular expression in which the g
(global
) flag is set:
var pattern:RegExp = /\w\d/g; var str:String = "a1 b2 c3 d4"; pattern.lastIndex = 2; trace(pattern.exec(str)); // b2 trace(pattern.lastIndex); // 5 trace(pattern.exec(str)); // c3 trace(pattern.lastIndex); // 8 trace(pattern.exec(str)); // d4 trace(pattern.lastIndex); // 11 trace(pattern.exec(str)); // null
multiline | property |
multiline:Boolean
[read-only]
Language version: | ActionScript 3.0 |
Runtime version: |
Specifies whether the m
(multiline
) flag is set. If it is set,
the caret (^
) and dollar sign ($
) in a regular expression
match before and after new lines.
Use the m
flag when constructing a regular expression to set
multiline = true
.
public function get multiline():Boolean
m
(multiline
) flag:
var pattern:RegExp = /^bob/; var str:String = "foo\n" + "bob"; trace(pattern.multiline); // false trace(pattern.exec(str)); // null pattern = /^bob/m; trace(pattern.multiline); // true trace(pattern.exec(str)); // bob
source | property |
source:String
[read-only]
Language version: | ActionScript 3.0 |
Runtime version: |
Specifies the pattern portion of the regular expression.
Implementation public function get source():String
source
parameter for two regular expressions:
var re1:RegExp = /aabb/gi; trace (re1.source); // aabb var re2:RegExp = new RegExp("x+y*", "i"); trace(re2.source); // x+y*
RegExp | () | constructor |
public function RegExp(re:String, flags:String)
Language version: | ActionScript 3.0 |
Runtime version: |
Lets you construct a regular expression from two strings. One string defines the pattern of the regular expression, and the other defines the flags used in the regular expression.
Parametersre:String — The pattern of the regular expression (also known as the constructor string). This is the
main part of the regular expression (the part that goes within the "/" characters).
Notes:
|
|
flags:String — The modifiers of the regular expression. These can include the following:
All other characters in the |
exec | () | method |
AS3 function exec(str:String):Object
Language version: | ActionScript 3.0 |
Runtime version: |
Performs a search for the regular expression on the given string str
.
If the g
(global
) flag is not set for the regular
expression, then the search starts
at the beginning of the string (at index position 0); the search ignores
the lastIndex
property of the regular expression.
If the g
(global
) flag is set for the regular
expression, then the search starts
at the index position specified by the lastIndex
property of the regular expression.
If the search matches a substring, the lastIndex
property changes to match the position
of the end of the match.
str:String — The string to search.
|
Object —
If there is no match, null ; otherwise, an object with the following properties:
|
See also
test | () | method |
AS3 function test(str:String):Boolean
Language version: | ActionScript 3.0 |
Runtime version: |
Tests for the match of the regular expression in the given string str
.
If the g
(global
) flag is not set for the regular expression,
then the search starts at the beginning of the string (at index position 0); the search ignores
the lastIndex
property of the regular expression.
If the g
(global
) flag is set for the regular expression, then the search starts
at the index position specified by the lastIndex
property of the regular expression.
If the search matches a substring, the lastIndex
property changes to match the
position of the end of the match.
str:String — The string to test.
|
Boolean —
If there is a match, true ; otherwise, false .
|
test()
method on a regular
expression in which the g
(global
) flag is set:
var re1:RegExp = /\w/g; var str:String = "a b c"; trace (re1.lastIndex); // 0 trace (re1.test(str)); // true trace (re1.lastIndex); // 1 trace (re1.test(str)); // true trace (re1.lastIndex); // 3 trace (re1.test(str)); // true trace (re1.lastIndex); // 5 trace (re1.test(str)); // false
informalizeGreeting()
method simply replaces the word Hello
with Hi
,
regardless of case. It also strips out the surname in the name in the string
(assuming that name matches the specified pattern). In the validateEmail()
and
validatePhoneNumber()
methods, the string passed is checked to see if its pattern matches a valid
email address or a specific phone number pattern, and the methods return Boolean values based on the results.
package { import flash.display.Sprite; public class RegExpExample extends Sprite { public function RegExpExample() { var formalGreeting:String = "Hello, John Smith."; trace(informalizeGreeting(formalGreeting)); // Hi, John. var validEmail:String = "name@domain.com"; trace(validateEmail(validEmail)); // true var invalidEmail:String = "foo"; trace(validateEmail(invalidEmail)); // false var validPhoneNumber:String = "415-555-1212"; trace(validatePhoneNumber(validPhoneNumber)); // true var invalidPhoneNumber:String = "312-867-530999"; trace(validatePhoneNumber(invalidPhoneNumber)); // false } private function informalizeGreeting(str:String):String { var pattern:RegExp = new RegExp("hello, (\\w+) \\w+", "i"); return str.replace(pattern, "Hi, $1"); } private function validateEmail(str:String):Boolean { var pattern:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/; var result:Object = pattern.exec(str); if(result == null) { return false; } return true; } private function validatePhoneNumber(str:String):Boolean { var pattern:RegExp = /^\d{3}-\d{3}-\d{4}$/; var result:Object = pattern.exec(str); if(result == null) { return false; } return true; } } }