privatevar domainName:RegExp = /(ftp|http|https|file):////[/S]+(/b|$)/gim; privatefunction matchDomain():void { var s:String = "Hello my domain is http://www.bar.com, but I a lso like http://foo.net as well as www.baz.org"; var replacedString = (s.replace(domainName, '<a href="$&">$&</a>').replace(/([^//])(www[/S]+(/b|$))/gim, '$1<a href="http://$2">$2</a>')); }
最后, 你会得出以下结果: 代码如下: Hello my domain is <a href="http://www.bar.com">http://www.bar.com</a>, but I also like <a href="http://foo.net">http://foo.net</a> as well as www.baz.org 显然地, 这个结果并不合乎理想。在原先的RegExp 中, 只有那些以ftp, http, https 或file 开头的字符串才会被匹配, 而www.baz.org 这个字符串则不合乎条件。以下的replace 语句会在所有匹配结果中, 寻找那些包含”www”但没有”/”号在它跟前的字符串。