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”但沒有”/”號在它跟前的字符串。