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:

`/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:

• Creating a regular expression pattern

• Using special characters in patterns

• Identifying sequences of multiple characters (such as “a two-digit number” or “between seven and ten letters”)

• Identifying any character in a range of letters or numbers (such as “any letter from a to m” )

• Identifying a character in a set of possible characters

• Identifying subsequences (segments within a pattern)

• Matching and replacing text based on patterns

Important concepts and terms

The following reference list contains important terms used in this chapter:

• Escape character: A character indicating that the character that follows should be treated as a metacharacter rather than a literal character. In regular expression syntax, the backslash character (\) is the escape character, so a backslash followed by another character is a special code rather than just the character itself.

• Flag: A character that specifies some option about how the regular expression pattern should be used, such as whether to distinguish between uppercase and lowercase characters.

• Metacharacter: A character that has special meaning in a regular expression pattern, as opposed to literally representing that character in the pattern.

• Quantifier: A character (or several characters) indicating how many times a part of the pattern should repeat. For example, a quantifier would be used to designate that a United States postal code should contain five or nine numbers.

• Regular expression: A program statement defining a pattern of characters that can be used to confirm whether other strings match that pattern or to replace portions of a string.

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:

1. Create a new Flash document.

2. Select a keyframe and open the Actions panel.

3. Create a RegExp (regular expression) variable such as this one:

`var pattern:RegExp = /ABC/;`
4. Copy the pattern from the example and assign it as the value of your RegExp variable. For instance, in the previous line of code, the pattern is the part of the code to the right of the equals sign, not including the semicolon (/ABC/).

5. Create one or more String variables containing strings appropriate for testing your regular expression. For instance, if you are creating a regular expression to test for valid e-mail addresses, create a few String variables containing valid and bad e-mail addresses:

```var goodEmail:String = "bob@example.com";
```trace(goodEmail, " is valid:", pattern.test(goodEmail));
```bob@example.com is valid: true