I have a text that I want to linkify (identify URLs and convert them to HTML links). The text could be multi-line, and could contain multiple urls like the example below.
我有一个我想要链接的文本(识别URL并将它们转换为HTML链接)。文本可以是多行的,并且可以包含多个URL,如下例所示。
My current actionscript code looks like this
我当前的动作脚本代码如下所示
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private function init():void {
var str:String = "@stack the website for google is http://www.google.com and gmail is http://gmail.com";
//Alert.show(linkify(str),"Error");
txtStatus.htmlText = linkify(str);
}
private function linkify(texty:String):String {
//return texty.replace("/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g",function(m):String { return m.linkify(m);});
//return texty.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function(m):String {return m.linkify(m);}).replace(/(^|[^\w])(@[\d\w\-]+)/g, function(m2):String{return '@<a href="http://twitter.com/' + m2.substr(1) + '">' + m2.substr(1) + '</a>'; });
var pattern:RegExp = /[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g;
var match:String = pattern.exec(texty);
return texty.replace(pattern,'<a href="' + match + '">' +
match + '</a>');
}
]]>
</mx:Script>
The problem with the above script is that it recognizes the first match and uses that across. Also how do i do it for @
?
上述脚本的问题在于它识别第一个匹配并使用它。另外我怎么做@?
Any help is highly appreciated.
任何帮助都非常感谢。
1 个解决方案
#1
ooph ... why does everybody use regex these days, to accomplish super simple tasks? also, you forgot, that "+" is a valid character for URLs, as a replacement for space, and even an awful lot of other characters may be used, so your pattern would not even match accordingly ...
ooph ...为什么现在每个人都使用正则表达式来完成超级简单的任务?另外,你忘了,“+”是URL的有效字符,作为空间的替代品,甚至可能会使用很多其他字符,因此你的模式甚至不会相应地匹配......
well, anyway, have a look at AS3 regex metacharacters ... that'll GREATLY improve your expression's readability and is much more robust...
好吧,无论如何,看看AS3正则表达式元字符...这将极大地提高你的表达的可读性,并且更加健壮......
i'd go with something like this, really:
我会选择这样的东西,真的:
var r:RegExp = /(?:http|https):\/\/\S*/g;
trace(str.replace(r, function (s:String,...rest):String {
return '<a href="' + s + '">' + s + '</a>'
} ));
but the actual point, was the global
flag ...
但实际的一点是,全球旗帜......
good luck then ... :)
那么祝你好运吧 ... :)
greetz
back2dos
#1
ooph ... why does everybody use regex these days, to accomplish super simple tasks? also, you forgot, that "+" is a valid character for URLs, as a replacement for space, and even an awful lot of other characters may be used, so your pattern would not even match accordingly ...
ooph ...为什么现在每个人都使用正则表达式来完成超级简单的任务?另外,你忘了,“+”是URL的有效字符,作为空间的替代品,甚至可能会使用很多其他字符,因此你的模式甚至不会相应地匹配......
well, anyway, have a look at AS3 regex metacharacters ... that'll GREATLY improve your expression's readability and is much more robust...
好吧,无论如何,看看AS3正则表达式元字符...这将极大地提高你的表达的可读性,并且更加健壮......
i'd go with something like this, really:
我会选择这样的东西,真的:
var r:RegExp = /(?:http|https):\/\/\S*/g;
trace(str.replace(r, function (s:String,...rest):String {
return '<a href="' + s + '">' + s + '</a>'
} ));
but the actual point, was the global
flag ...
但实际的一点是,全球旗帜......
good luck then ... :)
那么祝你好运吧 ... :)
greetz
back2dos