Flash CS4 Resources
Basics of regular expressions
Introduction to using 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:
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:
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.
Common regular expression tasks
There are several common uses for regular expressions, which are described in detail in this chapter:
Important concepts and terms
The following reference list contains important terms used in this chapter:
Working through in-chapter examples
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: