Flash CS3 Documentation |
|||
| Programming ActionScript 3.0 > Using regular expressions > Basics of regular expressions | |||
A regular expression describes a pattern of characters. Regular expressions are typically used to verify that a text value conforms to a particular pattern (such as verifying that a user-entered phone number has the proper number of digits) or to replace portions of a text value that matches a particular pattern.
Regular expressions can be simple. For example, suppose you wanted to confirm that a particular string matches "ABC," or wanted to replace every occurrence of "ABC" in a string with some other text. In that case, you could use the following regular expression, which defines the pattern consisting of the letters A, B, and C in sequence:
/ABC/
Note that the regular expression literal is delineated with the forward slash (/) character.
Regular expression patterns can also be complex, and sometimes cryptic in appearance, such as the following expression to match a valid e-mail address:
/([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}/
Most commonly you will use regular expressions to search for patterns in strings and to replace characters. In those cases, you will create a regular expression object and use it as a parameter for one of several String class methods. The following methods of the String class take regular expressions as parameters: match(), replace(), search(), and split(). For more information on these methods, see Finding patterns in strings and replacing substrings.
The RegExp class includes the following methods: test() and exec(). For more information, see Methods for using regular expressions with strings.
There are several common uses for regular expressions, which are described in detail in this chapter:
The following reference list contains important terms used in this chapter:
\) is the escape character, so a backslash followed by another character is a special code rather than just the character itself.As you're working through the chapter, you may want to test some of the example code listings for yourself. Because the code listings in this chapter consist primarily of regular expression patterns, testing the examples involves a few steps:
var pattern:RegExp = /ABC/;
/ABC/).var goodEmail:String = "bob@example.com"; var badEmail:String = "5@$2.99";
trace() function or by writing them to a text field on the Stage.trace(goodEmail, " is valid:", pattern.test(goodEmail)); trace(badEmail, " is valid:", pattern.test(badEmail));
For instance, assuming pattern defines the regular expression pattern for a valid e-mail address, the preceding lines of code writes this text to the Output panel:
bob@example.com is valid: true 5@$2.99 is valid: false
For more information about testing values by writing the values into a text field instance on the Stage or by using the trace() function to print the values to the Output panel, see Testing in-chapter example code listings.
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000111.html