package //com.example.programmingas3.wikiEditor { /** * A utility class that converts URL strings, such as "http://example.com" to * HTML anchor links, such as "http://example.com. */ public class actionscript_book_WikiEditor_com_example_programmingas3_wikiEditor_URLParser { /** * Converts HTTP and FTP URLs to anchor links. This function assembles a * RegExp pattern out of multiple parts: protocol, urlPart, and optionalUrlPart. */ public static function urlToATag(input:String):String { /** * Matches either http:// or ftp://. (?: indicates that the interior group * is not a capturing group. */ var protocol:String = "((?:http|ftp)://)"; /** * For the URL http://foo.example.com/foo, matches foo.example. */ var urlPart:String = "([a-z0-9_-]+\.[a-z0-9_-]+)"; /** * For the URL http://example.com/foo, matches example. */ var optionalUrlPart:String = "(\.[a-z0-9_-]*)"; /** * Assembles the pattern from its component parts. */ var urlPattern:RegExp = new RegExp (protocol + urlPart + optionalUrlPart, "ig"); /** * Replaces matching URL strings with a replacement string. The call to * the replace() method uses references to captured groups (such as $1) * to assemble the replacement string. */ var result:String = input.replace(urlPattern, "$1$2$3"); /** * Next, find e-mail patterns and replace them with hyperlinks. */ result = emailToATag(result); return result; } /** * Replaces an e-mail pattern with a corresponding HTML anchor hyperlink. * Like the urlToATag() method, this method assembles a regular expression out * of constituent parts. */ public static function emailToATag(input:String):String { /** * Isolates the mailto: part of the string. */ var protocol:String = "(mailto:)"; /** * Matches the name and @ symbol, such as bob.fooman@. */ var name:String = "([a-z0-9_-]+(?:\.[a-z0-9_-])*@)"; /** * For the e-mail pattern bob.fooman@mail.example.com, matches * mail.example. (including the trailing dot). */ var domain:String = "((?:[a-z0-9_-].)*)"; /** * Matches the superdomain, such as com, uk, or org., which is 2 - 4 letters. */ var superDomain:String = "([a-z]{2,4})"; /** * Assembles the matching regular expression out of constituent parts. */ var emailPattern:RegExp = new RegExp (protocol + name + domain + superDomain, "ig"); /** * Replaces matching e-mail strings with a replacement string. The call to * the replace() method uses references to captured groups (such as $1) * to assemble the replacement string. */ var result:String = input.replace(emailPattern, "$1$2$3$4"); return result; } } }