Recipe 13.4. Removing and Replacing Characters and Words Using Patterns


Problem

You want to remove characters from a string that matches a regex, or you want to replace pattern matches with a different substring.

Solution

Use the String.replace( ) method.

Discussion

The String.replace( ) method (see Recipe 12.7) allows you to use a string pattern to replace characters and words. Instead of using a string to define the pattern to replace, a more powerful approach is to use a regular expression.

You'll recall from Recipe 12.7 that the replace( ) method takes two parameters:


pattern

The substring or regular expression you want to find and replace.


replace

The value with which to replace each pattern match. This value is typically a string, but can also be a function that returns a string; this is particularly useful when pattern is a regular expression.

Using the replace( ) method is the same as what you're already familiar with, except instead of using a string pattern, you use a regex. Remember that replace( ) does not modify the original string, so make sure you use the string returned by replace( ) to work with the result:

var example:String = "<p>A string with <b>HTML</b> in it</p>"; // Replace the HTML by using a non-greedy global regex, using the empty // string as the replacement substring for every match to the pattern. example = example.replace( /<.*?>/g, "" ); // Displays: A string with HTML in it trace( example );

The preceding code demonstrates how to remove HTML tags within a string. The use of a nongreedy regex is required, as discussed in Recipe 13.5. By using the empty string as the replacement string for all pattern matches, the result is the pattern matches being removed from the original string.

The RegExp's global flag determines how the replace( ) method behaves. When the flag is set, all instances of the pattern are replaced. When the flag is not set, only the first occurrence of the pattern is replaced.

Using a regular expression as the pattern parameter makes the replace parameter more powerful as well. When the replace parameter is a string, there are special replacement codes that exist to allow parts of the pattern to be used as the replacement. Table 13-4 lists the replacement codes that can appear in the replace string, as well as what the code is replaced by.

Table 13-4. Replacement codes
CodeIs replaced by
                               $$

The $ character.
                               $&

The matched substring.
                               $\Q

The text in the string that precedes the matched substring. Note that the character after the $ is an accent grave, usually found just to the left of the 1 key and above the tab key.
                               $'

The text in the string that follows the matched substring.
                               $n                            

The nth group match when n ranges from 19.
                               $nn                            

The nnth group match where nn is a two-digit number from 10-99.


The replacement codes give you a powerful way to replace text. Say you have a plain text string and you want to replace every link starting with http:// with its HTML equivalent, surrounding the link with anchor tags and setting the href attribute correctly. This task is virtually impossible when using a string as the pattern, but with a regex pattern and the replacement codes that replace accepts, the desired effect is achieved much more easily:

var example:String = "Visit me at http://www.darronschall.com, or Adobe at "                    + "http://www.adobe.com."; // Replace links starting with http:// and ending in .com, .net,  // or .org with the matched subtstring wrapped inside of an anchor  // tag, using the match as both the href attribute and the link text example = example.replace( /http:\/\/.*?\.(com|net|org)/g, "<a href=\"$&\">$&</a>" ); /* Displays: Visit me at <a href="http://www.darronschall.com">http://www.darronschall.com</a>, or Adobe at <a href="http://www.adobe.com">http://www.adobe.com</a>. */ trace( example );

Although replacement codes are valuableand let you do creative and useful thingsperhaps a more powerful method to replace text is to use a replacement function as the replace parameter. The replacement function is invoked every time a match to the pattern is found and the string value returned by the function is used as the replacement value. The replacement function is passed a varying number of arguments that can be used to generate the appropriate string replacement for the match. The parameters passed to the function, in order, are:

  • The matched substring

  • The matched groups, dependant upon the number of groups that appear in the pattern, one parameter for each group; use arguments.length 3 to determine the number of matching group substrings passed into the function

  • The starting index of the matched substring within the string

  • The string replace( ) was called on

Because the number of groups in the regex could vary, there isn't a strongly typed method header to use. Instead, you have to declare the replacement function as taking no arguments and rely on the arguments array to get the information you need:

var example:String = "Visit me at http://www.darronschall.com, or Adobe at "                    + "http://www.adobe.com."; // Replace all links according to the replaceLinks function, which  // determines what the text for the anchor tag should say example = example.replace( /http:\/\/.*?\.(com|net|org)/g, replaceLinks ); /* Displays: Visit me at <a href="http://www.darronschall.com">my website</a>,  or Adobe at <a href="http://www.adobe.com">their website</a>. */ trace( example );

You can then define replaceLinks( ) as follows.

public function replaceLinks(  ):String {   var linkText:String;   // Whenever the link being replaced is darronschall.com, use    // "my website" as the text value for the anchor tag, otherwise    // use "their website"   if ( arguments[0] == "http://www.darronschall.com" ) {     linkText = "my website";       } else {     linkText = "their website";   }   // Construct an HTML link from the matched text using the    // appropriate link text   return "<a href=\"" + arguments[0] + "\">" + linkText + "</a>"; }

By using a replacement function, the preceding code conditionally changed the text in the anchor tag based on the value of the matched substring. This type of functionality goes above and beyond what replacement codes are able to accomplish.

See Also

Recipes 12.7 and 13.5




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