Working with Regular Expressions


You can work with regular expressions by way of the RegExp methods or by way of the String methods that accept RegExp parameters. The methods are as follows.

  • RegExp.test()

  • RegExp.exec()

  • String.search()

  • String.replace()

  • String.match()

We'll next look at using these methods in the following sections.

Boolean Testing Regular Expressions

The test() method is a method of the RegExp class, and it accepts a string parameter. The test() method returns true if the string parameter contains a substring that matches the regular expression pattern. Otherwise it returns false. The following example uses a regular expression that matches one or more lowercase characters. As you can see from the comments, the test() method returns true or false depending on whether or not the regular expression matches a substring in the string parameter.

var pattern:RegExp = /[a-z]+/; var string:String = "abcd"; trace(pattern.test(string));  // true string = "1234"; trace(pattern.test(string));  // false


Finding Matching Substring Indices

The search() method is a method of the String class, and it accepts a regular expression parameter. The search() method returns the index of the first matching substring. The following example locates the index of the first word that starts with the letter p:

var pattern:RegExp = /\bp[a-z]+/; var string:String = "There is no path to peace. Peace is the path."; trace(string.search(pattern));  // 12


The search() method always starts searching from the start of the string. The global flag and lastIndex property have no effect on search().

Retrieving Matching Substrings

The RegExp.exec() method and the String.match() method both enable you to retrieve matching substrings using regular expressions. However, the two methods operate in different ways.

The exec() method returns an array containing the substring matched. As discussed in the section, "Using Regular Expression Groups," later in this chapter, the array returned by exec() can also contain group subpattern matches.

Even when the global flag is set, the exec() method only ever finds one match at a time. The exec() method sets the lastIndex property of the RegExp object from which it was called to the index immediately following the most recent matching substring. This arrangement allows you to continue to apply the exec() method to a string, and it finds the next matching substring each time. The exec() method uses the lastIndex property to determine that index from which to start the next search. When no more matching substrings are found, exec() returns null and resets lastIndex to 0. The following code illustrates the use of exec() as well as lastIndex. When you run the code, you can see that after null is returned, the lastIndex property is reset to 0. Following that, the method searches from the start of the string once again. The following example outputs each word within the string, one at a time, along with the index of the last character in the string that was previously examined:

var pattern:RegExp = /[a-z]+/ig; var string:String = "There is no path to peace. Peace is the path."; for(var i:uint = 0; i < 12; i++) {     trace(pattern.exec(string));     trace(pattern.lastIndex);  }


The lastIndex property is a read-write property. That means you can set lastIndex so that it specifies the starting index from which exec() ought to start searching.

The match() method returns an array of all the substrings that match the pattern. Use the global flag to match all substrings. If you don't set the global flag, the match() method returns an array with only the first matching substring. The following example illustrates the use of the match() method.

// This expression will match strings that follow the rules of email // address values. var pattern:RegExp = /(?:\w|[_.\-])+@(?:(?:\w|-)+\.)+\w{2,4}/g; var string:String = "emails: user@domain.com, user@server.com, email@example.com"; trace(string.match(pattern)); // user@domain.com, user@server.com, email@example.com


Replacing Substrings Using Regular Expressions

The replace() method is a String method that enables you to replace substrings using regular expressions. When using regular expressions with the replace() method, use the global flag if you want to replace all instances of a pattern or it will only replace the first instance. The following example replaces all email address with <email>@<domain>.com.

var pattern:RegExp = /((?:\w|[_.\-])+)@(?:((?:\w|-)+)\.)+\w{2,4}+/; var string:String = "The following was posted by user@domain.com."; trace(string.replace(pattern, "<email>@<domain>.com")); // outputs: The following was posted by <email>@<domain>.com.


For more complex replacements, you can specify a function reference for the second parameter in place of the string. That function is then passed the following parameters:

  • The matching substring

  • Capturing groups (see "Using Regular Expression Groups") (Note that if there are no capturing groups then these parameters are omitted.)

  • The index of the matching substring

  • The original string

The function should return a string. The string the function returns is what gets substituted.

The following example uses a replacement function:

package {    import flash.display.Sprite;        public class RegularExpressions extends Sprite {    public function RegularExpressions() {    // The ?: sequences make the groups non-capturing as discussed in     // the sections on groups in this chapter.    var pattern:RegExp = /(?:\w|[_.\-])+@(?:(?:\w|-)+\.)+\w{2,4}/;    var string:String = "The following was posted by user@domain.com.";    trace(string.replace(pattern, replacer));    // Prints out: The following was posted by user AT domain DOT com    }    // The regular expression doesn't have any capturing groups, so the    // function only expects three parameters.    private function replacer(match:String, index:int, originalString:String):String {    var string:String = match.replace("@", " AT ");    string = string.replace(/\./g, " DOT ");    return string;    }    } }





Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net