Recipe 13.2. Testing Regular Expressions


Problem

You want to test a regex against a string to determine if a match can be made.

Solution

Use the RegExp.test( ) and RexExp.exec( ) methods.

Discussion

Recipe 13.1 discusses the building blocks of regular expressions. Once you have created a regular expression that describes the pattern of what you want to find in a string, there are two methods from the RegExp class that can perform the search, test( ) and exec( ).

To determine whether a pattern can be matched against a string, use the test( ) method on a RegExp instance. The test( ) method takes the string being tested against as the parameter and returns a Boolean value of true if the pattern can be matched, and false otherwise:

// Create a pattern to match against a string var example:RegExp = /abc/; // Displays: true trace( example.test( "A string with abc in it" ) ); // Displays: true trace( example.test( "abc" ) ); // Displays: false trace( example.test( "Another string to test against..." ) );

The test( ) method returns true if the regular expression can be matched anywhere in the string, but it won't allow you to extract the match or determine where in the string the match was found. To retrieve this information, use the exec( ) method.

The exec( ) method works in much the same way that test( ) does, except that instead of returning a Boolean value, an array is returned containing the substring that matched the pattern. If no match is found, exec( ) returns null.

// Create a pattern to match against a string var example:RegExp = /abc/; // Save the result of a string with a substring that matches  // the pattern var result:Array = example.exec( "A string with abc in it" ); // Displays: abc trace( result ); // Displays: 14 trace( result.index ); // Save the result of a string that doesn't contain a match for  // the pattern result = example.exec( "A string with no match" ); // Displays null trace( result );

By default, the exec( ) starts searching from the beginning of the string and returns an array containing the first match that it finds, or null if no match is found. The array returned contains two properties when a match is found:

  • index, which points to the starting location in which the matching substring was found.

  • input, which is the string that the regex was executed against.

In most cases, the array returned from exec( ) contains a single elementthe substring that matched the regex. However, when parentheses are used in the pattern to group certain elements together, each substring that matches a group is another element in the returned result:

var example:RegExp = /(\d)abc(\d*)/; var result:Array = example.exec( "7abc" ); // Displays: 3 trace( result.length ); // Displays: 7abc trace( result[0] ); // Displays: 7 trace( result[1] ); // Displays:  trace( result[2] );

The pattern used for example contains two groupsone for "a digit" and another for "a digit repeated any number of times, including 0." After the exec( ) method is invoked, the result is an array containing three elements instead of just one.

  • The first element, at position 0, is the actual substring that matched the regular expression, in this case "7abc."

  • The second element, at position 1, is the string that matched the first parenthetical group \\d, which in this case is "7" because the group calls for a digit.

  • The third element, at position 2, is the empty string because the second group \\d*, which means "a digit repeated any number of times, including 0," was matched against the empty string.

As noted earlier, exec( ) always starts searching from the beginning of the target string, at position 0, to find a match. However, when a regex is flagged as global, the lastIndex property of the regex determines the starting position to look for a pattern match. Every time the exec( ) method is called on a global regex, the lastIndex property is automatically set to the position that immediately follows the last character in the matching substring. To match all occurrences of a pattern in a string, exec( ) needs to be called multiple times on a global regexone time for each match. Additionally, the lastIndex property can be set by hand to find a match after a specific position in the string.

The exec( ) method continues to cycle through the string with each call when a regex is flagged as global. After the method returns null, the regex returns to the beginning of the string for the next search.


// Create a global regex to match multiple times in a string var example:RegExp = /abc/g; // Search for the first match var result:Array = example.exec( "abc abc" ); // Displays 0 trace( result.index ); // Displays 3 trace( example.lastIndex ); // Search for the second match result = example.exec( "abc abc" ); // Displays 4 trace( result.index ); // Displays 7 trace( example.lastIndex ); // Search for a match again, but none is found so null is returned result = example.exec( "abc abc" ); // Displays: null trace( result ); // Displays: 0 trace( example.lastIndex );

After the first call to exec( ) in the preceding example, a match is found at index 0 and the lastIndex property is automatically set to 3. When exec( ) is called the second time, the regex starts looking for a match at position 3 and finds the second match at index 4 (automatically setting lastIndex to position 7). When exec( ) is called the final time, no match can be found and result is null. In this case, the lastIndex is set to 0 so that the next call to exec( ) starts at the beginning of the string.

You can use a while statement with the exec( ) method to find all the matches, like so:

// Create a regular expression that matches three-letter words var example:RegExp = /\b[a-z]{3}\b/g; var target:String = "This string has two three letter words"; /* Loop until the exec(  ) method returns null. This while loop outputs:    has    two */ var result:Array; while ( ( result = example.exec( target ) ) != null ) {   trace( result ); }

See Also

Recipes 13.1 and 13.3




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

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